private void TryEstablishConnection( )
        {
            if ( null == TcpClient ) {
                TcpClient = new TcpClient( );
            }

            while ( EndPointEnumerator.MoveNext( ) ) {
                CurrentEndPoint = EndPointEnumerator.Current;
                Debug.Print( "ServerConnector.TryEstablishConnection: trying {0}", CurrentEndPoint );
                OnConnectionAttemptStarted( CurrentEndPoint );

                try {
                    TcpClient.BeginConnect( CurrentEndPoint.Address, CurrentEndPoint.Port, ConnectCallback, null );
                    return;
                }
                catch ( NotSupportedException e ) {
                    Debug.Print( "ServerConnector.TryEstablishConnection: TcpClient.BeginConnect: caught NotSupportedException:\n{0}", e );
                }
                catch ( Exception e ) {
                    Debug.Print( "ServerConnector.TryEstablishConnection: TcpClient.BeginConnect: caught exception:\n{0}", e );
                }
            }

            if ( null != TcpClient ) {
                SocketRegistry.Unregister( TcpClient.Client.LocalEndPoint as IPEndPoint );
                TcpClient.Close( );
                TcpClient = null;
            }

            EndPointEnumerator.Dispose( );
            EndPoints = null;

            OnConnectionFailed( new Exception( "Ran out of IP addresses and ports to try." ) );
        }
 // Event dispatchers
 protected virtual void OnConnectionAttemptStarted( SslEndPoint sslEndPoint )
 {
     var handler = ConnectionAttemptStarted;
     if ( null != handler ) {
         handler( this, new ConnectionAttemptStartedEventArgs( sslEndPoint ) );
     }
 }
 protected virtual void OnConnectionAttemptFailed( SslEndPoint sslEndPoint, Exception e )
 {
     var handler = ConnectionAttemptFailed;
     if ( null != handler ) {
         handler( this, new ConnectionAttemptFailedEventArgs( sslEndPoint, e ) );
     }
 }
        protected virtual void Dispose( bool disposing )
        {
            if ( disposing ) {
                ConnectionAttemptStarted = null;
                ConnectionAttemptFailed = null;
                ConnectionFailed = null;
                ConnectionEstablished = null;

                EndPoints = null;
                IPAddresses = null;
                CurrentEndPoint = null;
                CertificateCollection = null;
                Configuration = null;

                if ( null != EndPointEnumerator ) {
                    EndPointEnumerator.Dispose( );
                    EndPointEnumerator = null;
                }
                if ( null != TcpClient ) {
                    TcpClient.Close( );
                    TcpClient = null;
                }
                if ( null != _protocolHandler ) {
                    _protocolHandler.Dispose( );
                    _protocolHandler = null;
                }
                if ( null != _sslStream ) {
                    _sslStream.Dispose( );
                    _sslStream = null;
                }
            }
        }
 public ConnectionAttemptStartedEventArgs( SslEndPoint sslEndPoint )
 {
     SslEndPoint = sslEndPoint;
 }
 public ConnectionAttemptFailedEventArgs( SslEndPoint sslEndPoint, Exception e )
 {
     SslEndPoint = sslEndPoint;
     Exception = e;
 }