if (response[0] != 0x00) {
if ((response[0] != 0x00) && (response[0] != 0x04)) {
ConnectSOCKS4(PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout){    NS_ENSURE_TRUE(fd, NS_ERROR_NOT_INITIALIZED);    NS_ENSURE_TRUE(addr, NS_ERROR_NOT_INITIALIZED);    unsigned char request[12];    int request_len = 0;    unsigned char response[10];    int response_len = 0;    request[0] = 0x04; // SOCKS version 4    request[1] = 0x01; // CD command code -- 1 for connect    // destination port    PRUint16 destPort = PR_htons(PR_NetAddrInetPort(addr));    request[2] = (unsigned char)(destPort >> 8);    request[3] = (unsigned char)destPort;    // destination IP     char * ip = nsnull;    // IPv4    if (PR_NetAddrFamily(addr) == PR_AF_INET)         ip = (char*)(&addr->inet.ip);    // IPv6    else if (PR_NetAddrFamily(addr) == PR_AF_INET6) {        // IPv4 address encoded in an IPv6 address        if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped))            ip = (char*)(&addr->ipv6.ip.pr_s6_addr[12]);        else {            LOGERROR(("IPv6 not supported in SOCK 4."));            return NS_ERROR_FAILURE;	// SOCKS 4 can't do IPv6        }    }    else {        LOGERROR(("Don't know what kind of IP address this is."));        return NS_ERROR_FAILURE;		// don't recognize this type    }    request[4] = *ip++;    request[5] = *ip++;    request[6] = *ip++;    request[7] = *ip++;    // username    request[8] = 'M';    request[9] = 'O';    request[10] = 'Z';    request[11] = 0x00;    request_len = 12;    int write_len = PR_Send(fd, request, request_len, 0, timeout);    if (write_len != request_len) {        LOGERROR(("PR_Send() failed. Wrote: %d bytes; Expected: %d.", write_len, request_len));        return NS_ERROR_FAILURE;    }        // get the server's response    response_len = 8;	// size of the response    response_len = PR_Recv(fd, response, response_len, 0, timeout);    if (response_len <= 0) {        LOGERROR(("PR_Recv() failed. response_len = %d.", response_len));        return NS_ERROR_FAILURE;    }        if (response[0] != 0x00) {        // it's not a SOCKS 4 reply or version 0 of the reply code        LOGERROR(("Not a SOCKS 4 reply. Expected: 0; received: %x.", response[0]));        return NS_ERROR_FAILURE;    }    if (response[1] != 0x5A) { // = 90: request granted        // connect request not granted        LOGERROR(("Connection request refused. Expected: 90; received: %d.", response[1]));        return NS_ERROR_FAILURE;    }     return NS_OK;}