'This function makes the forwarding of data by listening to two
sockets, and writing to one everything it reads on the other.
This is done using select(), in order to be able to listen on both sockets
simultaneously and to implement an inactivity timeout.'

def forward(self, client_sock, server_sock):

    log.debug(thread.get_ident(), 'Forwarding.')
    (octets_in, octets_out) = (0, 0)
    try:
        sockslist = [client_sock, server_sock]
        while 1:
            (readables, writeables, exceptions) = select.select(sockslist, [], [], 360)
            if (exceptions or ((readables, writeables, exceptions) == ([], [], []))):
                raise Connection_Closed
            data = ''
            for readable_sock in readables:
                writeableslist = [client_sock, server_sock]
                writeableslist.remove(readable_sock)
                data = readable_sock.recv(1024)
                if data:
                    writeableslist[0].send(data)
                    if (readable_sock == client_sock):
                        octets_out += len(data)
                    else:
                        octets_in += len(data)
                else:
                    raise Connection_Closed
    except socket.error:
        (exception, value, traceback) = sys.exc_info()
        if (value[0] == ERR_CONNECTION_RESET_BY_PEER):
            raise Connection_Closed
        raise
    finally:
        log.debug(thread.get_ident(), octets_in, 'octets    in    and', octets_out, 'octets    out.    Connection    closed.')
