public bool Release(ProxyConnection conn)
        {
            lock (this)
            {

                if (conn.clientSocket != null)
                {
                    WinTunnel.WriteTextToConsole(String.Format(" Releasing ProxyConnection#{0}: Client {1}, Server {2}.",
                        conn.connNumber,
                        conn.clientSocket.Client.RemoteEndPoint.ToString(),
                        conn.serverName+conn.serverPort));
                }
                else
                {
                    WinTunnel.WriteTextToConsole(string.Format("Releasing ProxyConnection#{0}: Server {1}.",
                        conn.connNumber,
                        conn.serverName + conn.serverPort));
                }

                conn.disconnect();
                m_connections.Remove(conn);

            }
            return true;
        }
 public ProxyConnection AddConnection( ProxyConnection conn)
 {
     lock(this)
     {
         WinTunnel.WriteTextToConsole(string.Format("Allocating ProxyConnection#{0} for new connection.", m_connCount));
         m_connections.Add(conn);
         conn.connNumber = m_connCount++;
     }
     return conn;
 }
        //Call back when the server listener socket has connected to a client request
        public static void DoAcceptTcpClientCallback(IAsyncResult ar)
        {
            ProxyConnection conn = null;

            try
            {

                ProxyClientListenerTask listener = (ProxyClientListenerTask)ar.AsyncState;
                if (listener.m_bContinue)
                {
                    listener.allDone.Set();

                    //create a new task for connecting to the server side.

                    conn = new ProxyConnection(m_bHttpsClient, m_bHttpsServer);

                    conn.clientSocket = listener.tcpListener.EndAcceptTcpClient(ar); //accept the client connection

                    WinTunnel.WriteTextToConsole(string.Format("Conn#{0} Accepted new connection. Local: {1}, Remote: {2}.",
                        conn.connNumber,
                        conn.clientSocket.Client.LocalEndPoint.ToString(),
                        conn.clientSocket.Client.RemoteEndPoint.ToString()));

                    conn.serverName = listener.m_serverName;
                    conn.serverPort = listener.m_serverPort;
                    IPHostEntry hostInfo = Dns.GetHostEntry(conn.serverName);
                    // Get the DNS IP addresses associated with the host.
                    IPAddress[] IPaddresses = hostInfo.AddressList;

                    conn.serverSocket = new TcpClient(AddressFamily.InterNetwork);
                    conn.serverSocket.BeginConnect(IPaddresses[0], conn.serverPort, new AsyncCallback(ProxySwapDataTask.connectForwardServerCallBack), conn);
                }
            }
            catch (SocketException se)
            {
                WinTunnel.WriteTextToConsole(string.Format("Conn# {0} Socket Error occurred when accepting client socket. Error Code is: {1}",
                    conn.connNumber, se.ErrorCode));
                if (conn != null && WinTunnel.connMgr!= null)
                {
                    WinTunnel.connMgr.Release(conn);
                }
            }
            catch (Exception e)
            {
                WinTunnel.WriteTextToConsole(string.Format("Conn# {0} Error occurred when accepting client socket. Error is: {1}",
                     conn.connNumber, e));
                if (conn != null && WinTunnel.connMgr != null)
                {
                    WinTunnel.connMgr.Release(conn);

                }
            }
        }
 public ProxySwapDataTask(ProxyConnection conn)
 {
     m_conn = conn;
 }