public void HandleCommandResponse(ReverseProxyConnectResponse response) { //a small prevention for calling this method twice, not required... just incase if (!_receivedConnResponse) { _receivedConnResponse = true; if (response.IsConnected) { this.HostName = response.HostName; //tell the Proxy Client that we've established a connection if (Type == ProxyType.HTTPS) { SendToClient(Encoding.ASCII.GetBytes("HTTP/1.0 200 Connection established\r\n\r\n")); } else if (Type == ProxyType.SOCKS5) { //Thanks to http://www.mentalis.org/soft/projects/proxy/ for the Maths try { List<byte> responsePacket = new List<byte>(); responsePacket.Add(SOCKS5_VERSION_NUMBER); responsePacket.Add(SOCKS5_CMD_REPLY_SUCCEEDED); responsePacket.Add(SOCKS5_RESERVED); responsePacket.Add(1); responsePacket.AddRange(response.LocalAddress.GetAddressBytes()); responsePacket.Add((byte)(Math.Floor((decimal)response.LocalPort / 256))); responsePacket.Add((byte)(response.LocalPort % 256)); SendToClient(responsePacket.ToArray()); } catch { //just incase the math failed //it will still show it's succesful SendToClient(new byte[] { SOCKS5_VERSION_NUMBER, SOCKS5_CMD_REPLY_SUCCEEDED, SOCKS5_RESERVED, 1, //static: it's always 1 0, 0, 0, 0, //bind ip 0, 0 //bind port }); } } _handshakeStream.Close(); ProxySuccessful = true; try { //start receiving data from the proxy Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, AsyncReceiveProxy, null); } catch { Disconnect(); } } else { if (Type == ProxyType.HTTPS) { Disconnect(); } else if (Type == ProxyType.SOCKS5) { //send a connection fail packet SendToClient(new byte[] { SOCKS5_VERSION_NUMBER, SOCKS5_CMD_REPLY_CONNECTION_REFUSED, SOCKS5_RESERVED, 1, //static: it's always 1 0, 0, 0, 0, //Bind Address 0, 0 //Bind Port }); } } Server.CallonUpdateConnection(this); } }