'This function reads the request socket for the request data, decodes
it and checks that it is well formed.'

def decode_request(self, data):

    if (len(data) < 9):
        raise Request_Invalid_Format(data)
    req = {}
    req['version'] = ord(data[0])
    if (req['version'] != SOCKS_VERSION):
        raise Request_Bad_Version(req)
    req['command'] = ord(data[1])
    if (not (req['command'] in COMMANDS)):
        raise Request_Unknown_Command(req)
    req['address'] = (socket.inet_ntoa(data[4:8]), string2port(data[2:4]))
    if (not is_port(req['address'][1])):
        raise Request_Invalid_Port(req)
    req['userid'] = data[8:].strip('\x00')
    return req
