def sock_type(sock):
    """
    We can thank Linux for this abomination of a function.
    
    As of Linux 2.6.27, bits 11-32 of the socket type are flags. This
    change was made with a complete lack of consideration for the fact
    that socket types are not flags, but are in fact "symbolic
    constants" represented on all major operating systems by sequential
    numbers. It is impossible to distinguish between some types (i.e.
    SOCK_STREAM and SOCK_RAW) using a simple bitwise AND, as one would
    expect from a flag value.
    
    To get around this, we treat the first 9 bits as the "real" type.
    """
    return sock.type & 1023
