private void SetupServerConnection( INetworkFacade serverConnection )
        {
            ServiceLog.Logger.Verbose( "{0} SessionContext::SetupServerConnection", Id );

            try
            {
                lock ( _changeServerConnectionMutex )
                {
                    //UnwireServerParserEvents();

                    //if ( ServerConnection != null )
                    //{
                    //    ServerConnection.BeginClose(
                    //        ( s, f ) => ServiceLog.Logger.Info( "{0} Server connection closed", Id ) );
                    //}

                    ServerConnection = serverConnection;

                    if ( serverConnection != null )
                    {
                        _remoteHosts.Add( new RemoteHost(){ Connection = serverConnection, HasDisconnected = false, HasStoppedSendingData = false} );

                        ServerParser = new HttpStreamParser();
                        ServerParser.AdditionalDataRequested += HandleServerParserAdditionalDataRequested;
                        ServerParser.PartialDataAvailable += HandleServerParserPartialDataAvailable;
                        ServerParser.ReadResponseHeaderComplete += HandleServerParserReadResponseHeaderComplete;
                        ServerParser.MessageReadComplete += HandleServerParserMessageReadComplete;
                        ServerConnection.ConnectionClosed += HandleServerConnectionConnectionClosed;

                        HasServerBegunShutdown = false;

                        ServerConnection.BeginReceive( HandleServerReceive );
                    }
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception(
                    string.Format( "{0} Unhandled exception setting up server connection", Id ), ex );
                ChangeState( SessionStateType.Error );
            }
        }
        public void Start( INetworkFacade connection )
        {
            Contract.Ensures( _clientConnection != null );
            Contract.Ensures( _parser != null );

            ServiceLog.Logger.Info( "{0} Starting new proxy client session with client ID {1}", Id, connection.Id );

            _parser = new HttpStreamParser();
            _parser.AdditionalDataRequested += HandleParserAdditionalDataRequested;
            _parser.ReadRequestHeaderComplete += HandleParserReadRequestHeaderComplete;
            _parser.PartialDataAvailable += HandleParserPartialDataAvailable;

            _clientConnection = connection;
            _clientConnection.ConnectionClosed += HandleClientConnectionClosed;

            _clientConnection.BeginReceive( ReceiveDataFromClient );

            _hasClientStoppedSendingData = false;
        }
        private void SetupClientConnection( INetworkFacade clientConnection )
        {
            ServiceLog.Logger.Info( "{0} SessionContext::SetupClientConnection", Id );

            try
            {
                lock ( _changeClientConnectionMutex )
                {
                    if (ClientParser != null)
                    {
                        ClientParser.AdditionalDataRequested -= HandleClientParserAdditionalDataRequested;
                        ClientParser.PartialDataAvailable -= HandleClientParserPartialDataAvailable;
                        ClientParser.ReadRequestHeaderComplete -= HandleClientParserReadRequestHeaderComplete;
                        ClientParser = null;
                    }

                    if (ClientConnection != null)
                    {
                        ClientConnection.ConnectionClosed -= HandleClientConnectionClosed;
                        ClientConnection.BeginClose(
                            ( s, f ) => ServiceLog.Logger.Info( "{0} Client connection closed", Id ) );
                    }

                    ClientConnection = clientConnection;

                    if ( ClientConnection != null )
                    {
                        ClientParser = new HttpStreamParser();
                        ClientParser.AdditionalDataRequested += HandleClientParserAdditionalDataRequested;
                        ClientParser.PartialDataAvailable += HandleClientParserPartialDataAvailable;
                        ClientParser.ReadRequestHeaderComplete += HandleClientParserReadRequestHeaderComplete;
                        ClientConnection.ConnectionClosed += HandleClientConnectionClosed;

                        HasClientBegunShutdown = false;

                        ClientConnection.BeginReceive( HandleClientReceive );
                    }
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception(
                    string.Format( "{0} Unhandled exception setting up client connection", Id ), ex );
                ChangeState( SessionStateType.Error );
            }
        }