import socket
import errno


def is_socket(fdesc):
    """Return true if fdesc is a socket.

    >>> is_socket(socket.socketpair()[0].makefile())
    True

    >>> is_socket(socket.socket().makefile())
    True

    >>> is_socket(open('/dev/null'))
    False

    """
    if fdesc.isatty():
        return False

    sock = socket.fromfd(fdesc.fileno(), socket.AF_UNIX, socket.SOCK_STREAM)
    try:
        sock.getsockname()
    except OSError as e:
        if e.errno == errno.ENOTSOCK:
            return False
    return True