'__negotiatesocks4(self,destaddr,destport)
Negotiates a connection through a SOCKS4 server.'

def __negotiatesocks4(self, destaddr, destport):

    rmtrslv = False
    try:
        ipaddr = socket.inet_aton(destaddr)
    except socket.error:
        if (self.__proxy[3] == True):
            ipaddr = '\x00\x00\x00\x01'
            rmtrslv = True
        else:
            ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
    req = (('\x04\x01' + self.__decode(struct.pack('>H', destport))) + ipaddr)
    if (self.__proxy[4] != None):
        req = (req + self.__proxy[4])
    req = (req + '\x00')
    if (rmtrslv == True):
        req = ((req + destaddr) + '\x00')
    self.sendall(req)
    resp = self.__recvall(8)
    if (resp[0] != '\x00'):
        self.close()
        raise GeneralProxyError((1, _generalerrors[1]))
    if (resp[1] != 'Z'):
        self.close()
        if (ord(resp[1]) in (91, 92, 93)):
            self.close()
            raise Socks4Error((ord(resp[1]), _socks4errors[(ord(resp[1]) - 90)]))
        else:
            raise Socks4Error((94, _socks4errors[4]))
    self.__proxysockname = (socket.inet_ntoa(resp[4:]), struct.unpack('>H', bytes(resp[2:4], 'utf8'))[0])
    if (rmtrslv != None):
        self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
    else:
        self.__proxypeername = (destaddr, destport)
