'Connect to remote and forward data between local and remote'

def handle(self):

    local_sock = self.connection
    if (self._local_linger_args is not None):
        (l_onoff, l_linger) = self._local_linger_args
        local_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', l_onoff, l_linger))
    if (self._remote_addr is not None):
        remote_dest_sock = remote_src_sock = socket.socket(family=self._remote_addr_family, type=self._remote_socket_type, proto=socket.IPPROTO_IP)
        remote_dest_sock.connect(self._remote_addr)
        _trace('%s    _TCPHandler    connected    to    remote    %s', datetime.utcnow(), remote_dest_sock.getpeername())
    else:
        (remote_dest_sock, remote_src_sock) = socket_pair()
    try:
        local_forwarder = threading.Thread(target=self._forward, args=(local_sock, remote_dest_sock))
        local_forwarder.setDaemon(True)
        local_forwarder.start()
        try:
            self._forward(remote_src_sock, local_sock)
        finally:
            local_forwarder.join()
    finally:
        try:
            try:
                _safe_shutdown_socket(remote_dest_sock, socket.SHUT_RDWR)
            finally:
                if (remote_src_sock is not remote_dest_sock):
                    _safe_shutdown_socket(remote_src_sock, socket.SHUT_RDWR)
        finally:
            remote_dest_sock.close()
            if (remote_src_sock is not remote_dest_sock):
                remote_src_sock.close()
