import socket


def uds_reachable(uds_path, return_sock=False):
    """ Check if the unix domain socket at path `uds_path` is reachable.

    Parameters
    ----------
    uds_path : str
    return_sock: bool, optional (default is False)
        Return the socket.

    Returns
    -------
    bool, socket
        If the socket is reachable, the socket if `return_sock` else None
        Remember to close the socket!
    """
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    try:
        sock.connect(uds_path)
    except (ConnectionRefusedError, FileNotFoundError):
        return False, None
    finally:
        if not return_sock:
            sock.shutdown(socket.SHUT_RDWR)
            sock.close()

    return True, sock if return_sock else None