if (firstTime) { nsSOCKSIOLayerIdentity		= PR_GetUniqueIdentity("SOCKS layer"); nsSOCKSIOLayerMethods		= *PR_GetDefaultIOMethods();  nsSOCKSIOLayerMethods.connect	= nsSOCKSIOLayerConnect; nsSOCKSIOLayerMethods.bind	= nsSOCKSIOLayerBind; nsSOCKSIOLayerMethods.acceptread = nsSOCKSIOLayerAcceptRead; nsSOCKSIOLayerMethods.getsockname = nsSOCKSIOLayerGetName; nsSOCKSIOLayerMethods.getpeername = nsSOCKSIOLayerGetPeerName; nsSOCKSIOLayerMethods.accept	= nsSOCKSIOLayerAccept; nsSOCKSIOLayerMethods.listen	= nsSOCKSIOLayerListen; nsSOCKSIOLayerMethods.close	= nsSOCKSIOLayerClose;  firstTime			= false; }   PRFileDesc *	sock; PRFileDesc *	layer; PRStatus	rv;   sock = PR_NewTCPSocket(); if (! sock) { return NS_ERROR_OUT_OF_MEMORY; }  layer = PR_CreateIOLayerStub(nsSOCKSIOLayerIdentity, &nsSOCKSIOLayerMethods); if (! layer) { PR_Close(sock); return NS_ERROR_FAILURE; }  nsSOCKSSocketInfo *infoObject = new nsSOCKSSocketInfo(); if (!infoObject) { PR_Close(sock);  return NS_ERROR_FAILURE; }  NS_ADDREF(infoObject); infoObject->SetProxyHost(proxyHost); infoObject->SetProxyPort(proxyPort); layer->secret = (PRFilePrivate*) infoObject; rv = PR_PushIOLayer(sock, PR_GetLayersIdentity(sock), layer);  if (rv == PR_SUCCESS) { *fd 	= sock; *info 	= infoObject; NS_ADDREF(*info); return NS_OK; }
if (firstTime) { nsSOCKSIOLayerIdentity		= PR_GetUniqueIdentity("SOCKS layer"); nsSOCKSIOLayerMethods		= *PR_GetDefaultIOMethods();  nsSOCKSIOLayerMethods.connect	= nsSOCKSIOLayerConnect; nsSOCKSIOLayerMethods.bind	= nsSOCKSIOLayerBind; nsSOCKSIOLayerMethods.acceptread = nsSOCKSIOLayerAcceptRead; nsSOCKSIOLayerMethods.getsockname = nsSOCKSIOLayerGetName; nsSOCKSIOLayerMethods.getpeername = nsSOCKSIOLayerGetPeerName; nsSOCKSIOLayerMethods.accept	= nsSOCKSIOLayerAccept; nsSOCKSIOLayerMethods.listen	= nsSOCKSIOLayerListen; nsSOCKSIOLayerMethods.close	= nsSOCKSIOLayerClose;  firstTime			= false; }   PRFileDesc *	sock; PRFileDesc *	layer; PRStatus	rv;   sock = PR_NewTCPSocket(); if (! sock) { return NS_ERROR_OUT_OF_MEMORY; }  layer = PR_CreateIOLayerStub(nsSOCKSIOLayerIdentity, &nsSOCKSIOLayerMethods); if (! layer) { PR_Close(sock); return NS_ERROR_FAILURE; }  nsSOCKSSocketInfo * infoObject = new nsSOCKSSocketInfo(); if (!infoObject) { PR_Close(sock);  PR_DELETE(layer); return NS_ERROR_FAILURE; }  NS_ADDREF(infoObject); infoObject->SetProxyHost(proxyHost); infoObject->SetProxyPort(proxyPort); layer->secret = (PRFilePrivate*) infoObject; rv = PR_PushIOLayer(sock, PR_GetLayersIdentity(sock), layer);  if (NS_FAILED(rv)) { PR_Close(sock); NS_RELEASE(infoObject); PR_DELETE(layer); return NS_ERROR_FAILURE; }  *fd = sock; *info = infoObject; NS_ADDREF(*info);
nsSOCKSIOLayerNewSocket(const char *host, 			PRInt32 port,			const char *proxyHost,			PRInt32 proxyPort,			PRFileDesc **fd, 			nsISupports** info){   if (firstTime)     {	nsSOCKSIOLayerIdentity		= PR_GetUniqueIdentity("SOCKS layer");	nsSOCKSIOLayerMethods		= *PR_GetDefaultIOMethods();		nsSOCKSIOLayerMethods.connect	= nsSOCKSIOLayerConnect;	nsSOCKSIOLayerMethods.bind	= nsSOCKSIOLayerBind;	nsSOCKSIOLayerMethods.acceptread = nsSOCKSIOLayerAcceptRead;	nsSOCKSIOLayerMethods.getsockname = nsSOCKSIOLayerGetName;	nsSOCKSIOLayerMethods.getpeername = nsSOCKSIOLayerGetPeerName;	nsSOCKSIOLayerMethods.accept	= nsSOCKSIOLayerAccept;	nsSOCKSIOLayerMethods.listen	= nsSOCKSIOLayerListen;	nsSOCKSIOLayerMethods.close	= nsSOCKSIOLayerClose;		firstTime			= false;     }         PRFileDesc *	sock;   PRFileDesc *	layer;   PRStatus	rv;      /* Get a normal NSPR socket */   sock = PR_NewTCPSocket();   if (! sock)      {	return NS_ERROR_OUT_OF_MEMORY;     }      layer = PR_CreateIOLayerStub(nsSOCKSIOLayerIdentity, &nsSOCKSIOLayerMethods);   if (! layer)     {	PR_Close(sock);	return NS_ERROR_FAILURE;     }      nsSOCKSSocketInfo *infoObject = new nsSOCKSSocketInfo();   if (!infoObject)     {	PR_Close(sock);	// clean up IOLayerStub	return NS_ERROR_FAILURE;     }      NS_ADDREF(infoObject);   infoObject->SetProxyHost(proxyHost);   infoObject->SetProxyPort(proxyPort);   layer->secret = (PRFilePrivate*) infoObject;   rv = PR_PushIOLayer(sock, PR_GetLayersIdentity(sock), layer);      if (rv == PR_SUCCESS)     {	*fd 	= sock;	*info 	= infoObject;	NS_ADDREF(*info);	return NS_OK;     }   PR_Close(sock);   return NS_ERROR_FAILURE;}