private static void HandshakeCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SmtpConnection.ConnectAndHandshakeAsyncResult asyncState = (SmtpConnection.ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             try
             {
                 LineInfo info = asyncState.connection.Reader.CurrentReader.EndReadLine(result);
                 if (info.StatusCode != SmtpStatusCode.ServiceReady)
                 {
                     asyncState.InvokeCallback(new SmtpException(info.StatusCode, info.Line, true));
                 }
                 else if (!asyncState.SendEHello())
                 {
                 }
             }
             catch (SmtpException)
             {
                 if (!asyncState.SendHello())
                 {
                 }
             }
         }
         catch (Exception exception)
         {
             asyncState.InvokeCallback(exception);
         }
     }
 }
 private static void AuthenticateContinueCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SmtpConnection.ConnectAndHandshakeAsyncResult asyncState = (SmtpConnection.ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             LineInfo info = AuthCommand.EndSend(result);
             if (info.StatusCode == ((SmtpStatusCode)0xeb))
             {
                 asyncState.connection.authenticationModules[asyncState.currentModule].CloseContext(asyncState.connection);
                 asyncState.connection.isConnected = true;
                 asyncState.InvokeCallback();
             }
             else
             {
                 if (info.StatusCode == ((SmtpStatusCode)0x14e))
                 {
                     asyncState.authResponse = info.Line;
                     if (!asyncState.AuthenticateContinue())
                     {
                         return;
                     }
                 }
                 asyncState.Authenticate();
             }
         }
         catch (Exception exception)
         {
             asyncState.InvokeCallback(exception);
         }
     }
 }
 private static void SendHelloCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SmtpConnection.ConnectAndHandshakeAsyncResult asyncState = (SmtpConnection.ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             HelloCommand.EndSend(result);
             asyncState.Authenticate();
         }
         catch (Exception exception)
         {
             asyncState.InvokeCallback(exception);
         }
     }
 }
 private static void SendEHelloCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SmtpConnection.ConnectAndHandshakeAsyncResult asyncState = (SmtpConnection.ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             try
             {
                 asyncState.connection.extensions = EHelloCommand.EndSend(result);
                 asyncState.connection.ParseExtensions(asyncState.connection.extensions);
                 if (asyncState.connection.pooledStream.NetworkStream is TlsStream)
                 {
                     asyncState.Authenticate();
                     return;
                 }
             }
             catch (SmtpException exception)
             {
                 if ((exception.StatusCode != SmtpStatusCode.CommandUnrecognized) && (exception.StatusCode != SmtpStatusCode.CommandNotImplemented))
                 {
                     throw exception;
                 }
                 if (!asyncState.SendHello())
                 {
                     return;
                 }
             }
             if (asyncState.connection.EnableSsl)
             {
                 if (!asyncState.connection.serverSupportsStartTls && !(asyncState.connection.pooledStream.NetworkStream is TlsStream))
                 {
                     throw new SmtpException(SR.GetString("MailServerDoesNotSupportStartTls"));
                 }
                 asyncState.SendStartTls();
             }
             else
             {
                 asyncState.Authenticate();
             }
         }
         catch (Exception exception2)
         {
             asyncState.InvokeCallback(exception2);
         }
     }
 }
 private static void ConnectionCreatedCallback(object request, object state)
 {
     SmtpConnection.ConnectAndHandshakeAsyncResult owningObject = (SmtpConnection.ConnectAndHandshakeAsyncResult)request;
     if (state is Exception)
     {
         owningObject.InvokeCallback((Exception)state);
     }
     else
     {
         SmtpPooledStream pooledStream = (SmtpPooledStream)((PooledStream)state);
         try
         {
             while ((pooledStream.creds != null) && (pooledStream.creds != owningObject.connection.credentials))
             {
                 owningObject.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, owningObject.connection.Timeout, false);
                 pooledStream = (SmtpPooledStream)owningObject.connection.connectionPool.GetConnection(owningObject, m_ConnectionCreatedCallback, owningObject.connection.Timeout);
                 if (pooledStream == null)
                 {
                     return;
                 }
             }
             if (Logging.On)
             {
                 Logging.Associate(Logging.Web, owningObject.connection, pooledStream);
             }
             pooledStream.Owner = owningObject.connection;
             pooledStream.creds = owningObject.connection.credentials;
             lock (owningObject.connection)
             {
                 if (owningObject.connection.isClosed)
                 {
                     owningObject.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, owningObject.connection.Timeout, false);
                     owningObject.InvokeCallback(null);
                     return;
                 }
                 owningObject.connection.pooledStream = pooledStream;
             }
             owningObject.Handshake();
         }
         catch (Exception exception)
         {
             owningObject.InvokeCallback(exception);
         }
     }
 }
 private static void SendStartTlsCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SmtpConnection.ConnectAndHandshakeAsyncResult asyncState = (SmtpConnection.ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             StartTlsCommand.EndSend(result);
             TlsStream stream = new TlsStream(asyncState.connection.pooledStream.ServicePoint.Host, asyncState.connection.pooledStream.NetworkStream, asyncState.connection.ClientCertificates, asyncState.connection.pooledStream.ServicePoint, asyncState.connection.client, asyncState.m_OuterResult.ContextCopy);
             asyncState.connection.pooledStream.NetworkStream = stream;
             asyncState.connection.responseReader             = new SmtpReplyReaderFactory(asyncState.connection.pooledStream.NetworkStream);
             asyncState.SendEHello();
         }
         catch (Exception exception)
         {
             asyncState.InvokeCallback(exception);
         }
     }
 }