BeginAccept() public method

public BeginAccept ( AsyncCallback callback, object state ) : IAsyncResult
callback AsyncCallback
state object
return IAsyncResult
        public bool OpenService()
        {
            this.streamServerHost = new StreamServerHost();

            var ep = streamServerHost.AddServiceEndpoint(typeof(IDataExchange), streamBinding, endpointRole, endpointVia);

            ep.Behaviors.Add(relayCreds);

            try
            {
                streamServerHost.Open();
                streamServerHost.BeginAccept(StreamAccepted, null);
            }
            catch (Exception e)
            {
                Trace.TraceError("Unable to connect: {0}", e.Message);
                return(false);
            }
            return(true);
        }
        public bool OpenService()
        {
            this.streamServerHost = new StreamServerHost();

            var ep = streamServerHost.AddServiceEndpoint(typeof(IDataExchange), streamBinding, endpointRole, endpointVia);
            ep.Behaviors.Add(relayCreds);

            try
            {
                streamServerHost.Open();
                streamServerHost.BeginAccept(StreamAccepted, null);
            }
            catch (Exception e)
            {
                Trace.TraceError("Unable to connect: {0}", e.Message);
                return false;
            }
            return true;
        }
        void StreamAccepted(IAsyncResult asyncResult)
        {
            try
            {
                StreamConnection streamConnection = streamServerHost.EndAccept(asyncResult);
                if (streamConnection != null)
                {
                    streamServerHost.BeginAccept(StreamAccepted, null);
                    if (streamConnection.ConnectionInfo.StartsWith("tcp:", StringComparison.OrdinalIgnoreCase))
                    {
                        int port;

                        if (!int.TryParse(streamConnection.ConnectionInfo.Substring(4), out port))
                        {
                            try
                            {
                                streamConnection.Stream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                        else
                        {
                            bool portAllowed = noPortConstraints;
                            Trace.TraceInformation("Incoming connection for port {0}", port);
                            if (!portAllowed)
                            {
                                for (int i = 0; i < allowedPorts.Count; i++)
                                {
                                    if (port == allowedPorts[i])
                                    {
                                        portAllowed = true;
                                        break;
                                    }
                                }
                            }
                            if (!portAllowed)
                            {
                                Trace.TraceWarning("Incoming connection for port {0} not permitted", port);
                                try
                                {
                                    streamConnection.Stream.Close();
                                }
                                catch (Exception ex)
                                {
                                    Trace.TraceError("Error closing stream: {0}", ex.Message);
                                }
                                return;
                            }
                        }
                    }
                    else if (streamConnection.ConnectionInfo.StartsWith("np:", StringComparison.OrdinalIgnoreCase))
                    {
                        string pipeName = streamConnection.ConnectionInfo.Substring(3);
                        Trace.TraceInformation("Incoming connection for pipe {0}", pipeName);

                        bool pipeAllowed = noPipeConstraints;
                        if (!pipeAllowed)
                        {
                            for (int i = 0; i < allowedPipes.Count; i++)
                            {
                                if (pipeName.Equals(allowedPipes[i], StringComparison.OrdinalIgnoreCase))
                                {
                                    pipeAllowed = true;
                                    break;
                                }
                            }
                        }
                        if (!pipeAllowed)
                        {
                            Trace.TraceWarning("Incoming connection for pipe {0} not permitted", pipeName);
                            try
                            {
                                streamConnection.Stream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                    }
                    else
                    {
                        Trace.TraceError("Unable to handle connection for {0}", streamConnection.ConnectionInfo);
                        streamConnection.Stream.Close();
                        return;
                    }

                    MultiplexConnectionInputPump connectionPump =
                        new MultiplexConnectionInputPump(streamConnection.Stream.Read,
                                                         OnCreateConnection,
                                                         streamConnection);
                    connectionPump.Run(false);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error accepting connection: {0}", ex.Message);
            }
        }