internal void ServiceRequest(object state)
        {
            ITransportHeaders      headers2;
            Stream                 stream2;
            ServerProcessing       processing;
            TcpServerSocketHandler handler        = (TcpServerSocketHandler)state;
            ITransportHeaders      requestHeaders = handler.ReadHeaders();
            Stream                 requestStream  = handler.GetRequestStream();

            requestHeaders["__CustomErrorsEnabled"] = handler.CustomErrorsEnabled();
            ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();

            sinkStack.Push(this, handler);
            WindowsIdentity             impersonationIdentity = handler.ImpersonationIdentity;
            WindowsImpersonationContext context = null;
            IPrincipal currentPrincipal         = null;
            bool       flag = false;

            if (impersonationIdentity != null)
            {
                currentPrincipal = Thread.CurrentPrincipal;
                flag             = true;
                if (this._impersonate)
                {
                    Thread.CurrentPrincipal = new WindowsPrincipal(impersonationIdentity);
                    context = impersonationIdentity.Impersonate();
                }
                else
                {
                    Thread.CurrentPrincipal = new GenericPrincipal(impersonationIdentity, null);
                }
            }
            try
            {
                try
                {
                    IMessage message;
                    processing = this._nextSink.ProcessMessage(sinkStack, null, requestHeaders, requestStream, out message, out headers2, out stream2);
                }
                finally
                {
                    if (flag)
                    {
                        Thread.CurrentPrincipal = currentPrincipal;
                    }
                    if (this._impersonate)
                    {
                        context.Undo();
                    }
                }
            }
            catch
            {
                throw;
            }
            switch (processing)
            {
            case ServerProcessing.Complete:
                sinkStack.Pop(this);
                handler.SendResponse(headers2, stream2);
                break;

            case ServerProcessing.OneWay:
                handler.SendResponse(headers2, stream2);
                break;

            case ServerProcessing.Async:
                sinkStack.StoreAndDispatch(this, handler);
                break;
            }
            if (processing != ServerProcessing.Async)
            {
                if (handler.CanServiceAnotherRequest())
                {
                    handler.BeginReadMessage();
                }
                else
                {
                    handler.Close();
                }
            }
        }
        } // TcpServerTransportSink

        internal void ServiceRequest(Object state)
        {
            TcpServerSocketHandler streamManager = (TcpServerSocketHandler)state;

            ITransportHeaders headers       = streamManager.ReadHeaders();
            Stream            requestStream = streamManager.GetRequestStream();

            headers["__CustomErrorsEnabled"] = streamManager.CustomErrorsEnabled();

            // process request
            ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();

            sinkStack.Push(this, streamManager);

            IMessage          responseMessage;
            ITransportHeaders responseHeaders;
            Stream            responseStream;

#if !FEATURE_PAL
            // If Impersonation was requested impersonate the client identity
            WindowsIdentity             identity = streamManager.ImpersonationIdentity;
            WindowsImpersonationContext context  = null;
            IPrincipal oldPrincipal     = null;
            bool       principalChanged = false;
            if (identity != null)
            {
                oldPrincipal     = Thread.CurrentPrincipal;
                principalChanged = true;
                if (_impersonate)
                {
                    Thread.CurrentPrincipal = new WindowsPrincipal(identity);
                    context = identity.Impersonate();
                }
                else
                {
                    Thread.CurrentPrincipal = new GenericPrincipal(identity, null);
                }
            }
#endif // !FEATURE_PAL
            ServerProcessing processing;
            // wrap Undo in an outer try block
            try{
                try{
                    processing =
                        _nextSink.ProcessMessage(sinkStack, null, headers, requestStream,
                                                 out responseMessage,
                                                 out responseHeaders, out responseStream);
                }
                finally{
    #if !FEATURE_PAL
                    // Revert the principal if we had changed the principal
                    if (principalChanged)
                    {
                        Thread.CurrentPrincipal = oldPrincipal;
                    }
                    // Revert the impersonation if we had impersonated
                    if (_impersonate)
                    {
                        context.Undo();
                    }
    #endif // !FEATURE_PAL
                }
            }
            catch { throw; }

            // handle response
            switch (processing)
            {
            case ServerProcessing.Complete:
            {
                // Send the response. Call completed synchronously.
                sinkStack.Pop(this);
                streamManager.SendResponse(responseHeaders, responseStream);
                break;
            } // case ServerProcessing.Complete

            case ServerProcessing.OneWay:
            {
                // No response needed, but the following method will make sure that
                //   we send at least a skeleton reply if the incoming request was
                //   not marked OneWayRequest (client/server metadata could be out of
                //   [....]).
                streamManager.SendResponse(responseHeaders, responseStream);
                break;
            } // case ServerProcessing.OneWay

            case ServerProcessing.Async:
            {
                sinkStack.StoreAndDispatch(this, streamManager);
                break;
            } // case ServerProcessing.Async
            } // switch (processing)


            // async processing will take care if handling this later
            if (processing != ServerProcessing.Async)
            {
                if (streamManager.CanServiceAnotherRequest())
                {
                    streamManager.BeginReadMessage();
                }
                else
                {
                    streamManager.Close();
                }
            }
        } // ServiceRequest
        } // TcpServerTransportSink

        internal void ServiceRequest(Object state)
        {
            TcpServerSocketHandler streamManager = (TcpServerSocketHandler)state;

            ITransportHeaders headers       = streamManager.ReadHeaders();
            Stream            requestStream = streamManager.GetRequestStream();

            headers["__CustomErrorsEnabled"] = streamManager.CustomErrorsEnabled();

            // process request
            ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();

            sinkStack.Push(this, streamManager);

            IMessage          responseMessage;
            ITransportHeaders responseHeaders;
            Stream            responseStream;

            ServerProcessing processing;

            // wrap Undo in an outer try block
            try{
                try{
                    processing =
                        _nextSink.ProcessMessage(sinkStack, null, headers, requestStream,
                                                 out responseMessage,
                                                 out responseHeaders, out responseStream);
                }
                finally{
                }
            }
            catch { throw; }

            // handle response
            switch (processing)
            {
            case ServerProcessing.Complete:
            {
                // Send the response. Call completed synchronously.
                sinkStack.Pop(this);
                streamManager.SendResponse(responseHeaders, responseStream);
                break;
            } // case ServerProcessing.Complete

            case ServerProcessing.OneWay:
            {
                // No response needed, but the following method will make sure that
                //   we send at least a skeleton reply if the incoming request was
                //   not marked OneWayRequest (client/server metadata could be out of
                //   sync).
                streamManager.SendResponse(responseHeaders, responseStream);
                break;
            } // case ServerProcessing.OneWay

            case ServerProcessing.Async:
            {
                sinkStack.StoreAndDispatch(this, streamManager);
                break;
            } // case ServerProcessing.Async
            } // switch (processing)


            // async processing will take care if handling this later
            if (processing != ServerProcessing.Async)
            {
                if (streamManager.CanServiceAnotherRequest())
                {
                    streamManager.BeginReadMessage();
                }
                else
                {
                    streamManager.Close();
                }
            }
        } // ServiceRequest