private SocketHandler CreateSocketHandler(
            Socket socket, SocketCache socketCache, String machinePortAndSid)
        {
            Stream netStream = new SocketStream(socket);

            // Check if authentication is requested
            if(_channel.IsSecured)
            {
                throw new NotSupportedException();
            }

            return new TcpClientSocketHandler(socket, machinePortAndSid, netStream, this);
        } // CreateSocketHandler
        } // AddHookChannelUri
        
        
        //
        // end of IChannelReceiverHook implementation
        //

        // Thread for listening
        void Listen()
        {
            bool bOkToListen = false;
        
            try
            {
                _tcpListener.Start(_bExclusiveAddressUse);
                bOkToListen = true;
            }
            catch (Exception e)
            {
                _startListeningException = e;
            }

            _waitForStartListening.Set(); // allow main thread to continue now that we have tried to start the socket                

            InternalRemotingServices.RemotingTrace( "Waiting to Accept the Socket on Port: " + _port);

            //
            // Wait for an incoming socket
            //
            Socket socket;
            
            while (bOkToListen)
            {
                InternalRemotingServices.RemotingTrace("TCPChannel::Listen - tcpListen.Pending() == true");                

                try
                {
                    socket = _tcpListener.AcceptSocket();

                    if (socket == null)
                    {
                        throw new RemotingException(
                            String.Format(
                                CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Socket_Accept"),
                                Marshal.GetLastWin32Error().ToString(CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        // disable nagle delay
                        socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
                        // Set keepalive flag, so that inactive sockets can be cleaned up
                        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);

                        // set linger option
                        LingerOption lingerOption = new LingerOption(true, 3);
                        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
                        
                        Stream netStream = new SocketStream(socket);
                        HttpServerSocketHandler streamManager = null;

                        //Create the socket Handler
                        streamManager = new HttpServerSocketHandler(socket, CoreChannel.RequestQueue, netStream);

                        // @



                        streamManager.DataArrivedCallback = new WaitCallback(_transportSink.ServiceRequest);
                        streamManager.BeginReadMessage();               
                    }
                } 
                catch (Exception e)
                {
                    if (!_bListening)
                    {
                        // We called Stop() on the tcp listener, so gracefully exit.
                        bOkToListen = false;                        
                    }
                    else
                    {
                        // we want the exception to show up as unhandled since this
                        //   is an unexpected failure.
                        if (!(e is SocketException))
                        {
                            // <



                        }
                    }
                }
            } // while (bOkToListen)
        }
 private SocketHandler CreateSocketHandler(Socket socket, SocketCache socketCache, string machinePortAndSid)
 {
     Stream netStream = new SocketStream(socket);
     if (this._channel.IsSecured)
     {
         netStream = this.CreateAuthenticatedStream(netStream, machinePortAndSid);
     }
     return new TcpClientSocketHandler(socket, machinePortAndSid, netStream, this);
 }
        // AcceptSocket method which will invoke the 
        // authorization callbacks
        void AcceptSocketCallback(IAsyncResult ar) 
        {
            Socket socket = null;
            InternalRemotingServices.RemotingTrace("TCPChannel::Listen - tcpListen.Pending() == true");
            TcpServerSocketHandler streamManager = null; 
            bool closeImmediately = true;
            try 
            { 
                //
                // Wait for an incoming socket 
                // if the listener is still active
                if (_tcpListener.IsListening)
                    _tcpListener.BeginAcceptSocket(_acceptSocketCallback, null);
 
                socket = _tcpListener.EndAcceptSocket(ar);
 
                if (socket == null) 
                {
                    throw new RemotingException( 
                        String.Format(
                            CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Socket_Accept"),
                            Marshal.GetLastWin32Error().ToString(CultureInfo.CurrentCulture)));
                } 

                if (_authorizeRemotingConnection != null) 
                { 
                        bool authorized = _authorizeRemotingConnection.IsConnectingEndPointAuthorized(socket.RemoteEndPoint);
                        if (!authorized) 
                            throw new RemotingException(CoreChannel.GetResourceString(
                                                                "Remoting_Tcp_ServerAuthorizationEndpointFailed"));
                }
 
                // disable nagle delay
                socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1); 
                // Set keepalive flag, so that inactive sockets can be cleaned up 
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
 
                // set linger option
                LingerOption lingerOption = new LingerOption(true, 3);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
 
                Stream netStream = new SocketStream(socket);
                streamManager = new TcpServerSocketHandler(socket, CoreChannel.RequestQueue, netStream); 
 
#if !FEATURE_PAL
                WindowsIdentity identity = null; 
#endif // !FEATURE_PAL
                // If authentication is requested wait for auth request.
                closeImmediately = false;
                if (_secure) 
                {
#if !FEATURE_PAL 
                    identity = Authenticate(ref netStream, streamManager); 
                    // Create a new SocketHandler to wrap the new netStream
                    streamManager = new TcpServerSocketHandler(socket, CoreChannel.RequestQueue, netStream); 
                    if (_authorizeRemotingConnection != null)
                    {
                        bool authorized = _authorizeRemotingConnection.IsConnectingIdentityAuthorized(identity);
                        if (!authorized) 
                            throw new RemotingException(CoreChannel.GetResourceString(
                                                                "Remoting_Tcp_ServerAuthorizationIdentityFailed")); 
                    } 
#else
                    throw new NotSupportedException(); 
#endif // !FEATURE_PAL
                }

#if !FEATURE_PAL 
                // Cache the identity for impersonation
                streamManager.ImpersonationIdentity = identity; 
#endif // !FEATURE_PAL 

 
                streamManager.DataArrivedCallback = new WaitCallback(_transportSink.ServiceRequest);
                streamManager.BeginReadMessage();
            }
            catch (Exception e) 
            {
                // Close the socket pre-emptively. We also close the socket if 
                // We need to catch all exceptions if we hit ObjectDisposedException 
                try{
                    if (streamManager != null){ 
                        streamManager.SendErrorResponse(e, false);
                    }
                    if (socket != null){
                        if (closeImmediately) 
                             socket.Close(0);
                        else 
                            socket.Close(); 
                    }
                }catch(Exception){} 
                if (!_bListening)
                {
                    // We called Stop() on the tcp listener, so gracefully exit.
                    //bOkToListen = false; 
                }
                else 
                { 
                    // we want the exception to show up as unhandled since this
                    //   is an unexpected failure. 
                    if (!(e is SocketException))
                    {
                        // <
 

 
                    } 
                }
            } 
        }
 private void AcceptSocketCallback(IAsyncResult ar)
 {
     Socket socket = null;
     TcpServerSocketHandler streamManager = null;
     bool flag = true;
     try
     {
         if (this._tcpListener.IsListening)
         {
             this._tcpListener.BeginAcceptSocket(this._acceptSocketCallback, null);
         }
         socket = this._tcpListener.EndAcceptSocket(ar);
         if (socket == null)
         {
             throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Socket_Accept"), new object[] { Marshal.GetLastWin32Error().ToString(CultureInfo.CurrentCulture) }));
         }
         if ((this._authorizeRemotingConnection != null) && !this._authorizeRemotingConnection.IsConnectingEndPointAuthorized(socket.RemoteEndPoint))
         {
             throw new RemotingException(CoreChannel.GetResourceString("Remoting_Tcp_ServerAuthorizationEndpointFailed"));
         }
         socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Debug, 1);
         socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
         LingerOption optionValue = new LingerOption(true, 3);
         socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, optionValue);
         Stream stream = new SocketStream(socket);
         streamManager = new TcpServerSocketHandler(socket, CoreChannel.RequestQueue, stream);
         WindowsIdentity identity = null;
         flag = false;
         if (this._secure)
         {
             identity = this.Authenticate(ref stream, streamManager);
             streamManager = new TcpServerSocketHandler(socket, CoreChannel.RequestQueue, stream);
             if ((this._authorizeRemotingConnection != null) && !this._authorizeRemotingConnection.IsConnectingIdentityAuthorized(identity))
             {
                 throw new RemotingException(CoreChannel.GetResourceString("Remoting_Tcp_ServerAuthorizationIdentityFailed"));
             }
         }
         streamManager.ImpersonationIdentity = identity;
         streamManager.DataArrivedCallback = new WaitCallback(this._transportSink.ServiceRequest);
         streamManager.BeginReadMessage();
     }
     catch (Exception exception)
     {
         try
         {
             if (streamManager != null)
             {
                 streamManager.SendErrorResponse(exception, false);
             }
             if (socket != null)
             {
                 if (flag)
                 {
                     socket.Close(0);
                 }
                 else
                 {
                     socket.Close();
                 }
             }
         }
         catch (Exception)
         {
         }
         if (this._bListening)
         {
             SocketException exception3 = exception as SocketException;
         }
     }
 }