def nl_connect(sk, protocol):
flags = getattr(socket, '<STR_LIT>', <NUM_LIT:0>)<EOL>if sk.s_fd != -<NUM_LIT:1>:<EOL><INDENT>return -NLE_BAD_SOCK<EOL><DEDENT>try:<EOL><INDENT>sk.socket_instance = socket.socket(getattr(socket, '<STR_LIT>', -<NUM_LIT:1>), socket.SOCK_RAW | flags, protocol)<EOL><DEDENT>except OSError as exc:<EOL><INDENT>return -nl_syserr2nlerr(exc.errno)<EOL><DEDENT>if not sk.s_flags & NL_SOCK_BUFSIZE_SET:<EOL><INDENT>err = nl_socket_set_buffer_size(sk, <NUM_LIT:0>, <NUM_LIT:0>)<EOL>if err < <NUM_LIT:0>:<EOL><INDENT>sk.socket_instance.close()<EOL>return err<EOL><DEDENT><DEDENT>try:<EOL><INDENT>sk.socket_instance.bind((sk.s_local.nl_pid, sk.s_local.nl_groups))<EOL><DEDENT>except OSError as exc:<EOL><INDENT>sk.socket_instance.close()<EOL>return -nl_syserr2nlerr(exc.errno)<EOL><DEDENT>sk.s_local.nl_pid = sk.socket_instance.getsockname()[<NUM_LIT:0>]<EOL>if sk.s_local.nl_family != socket.AF_NETLINK:<EOL><INDENT>sk.socket_instance.close()<EOL>return -NLE_AF_NOSUPPORT<EOL><DEDENT>sk.s_proto = protocol<EOL>return <NUM_LIT:0><EOL>
Create file descriptor and bind socket.

    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/nl.c#L96

    Creates a new Netlink socket using `socket.socket()` and binds the socket to the protocol and local port specified
    in the `sk` socket object (if any). Fails if the socket is already connected.

    Positional arguments:
    sk -- Netlink socket (nl_sock class instance).
    protocol -- Netlink protocol to use (integer).

    Returns:
    0 on success or a negative error code.