private void SendHandshakeCallback(IAsyncResult asyncResult) { AsyncHandshakeResult asyncHandshakeResult = (AsyncHandshakeResult)asyncResult.AsyncState; try { _recordStream.EndSend(asyncResult); lock (_handshakeLock) { if (_isAuthenticated && !_isHandshaking) { // Mark send result as complete asyncHandshakeResult.SetComplete(); return; } } // Handshake not finished yet, continue performing ProcessSendHandshakePacket(asyncHandshakeResult); } catch (AlertException ae) { ProcessSendFatalAlert(new Alert(ae.AlertDescription, _handshakeSession.NegotiatedVersion)); asyncHandshakeResult.SetComplete(new Exception("Connection closed because of local alert", ae)); } catch (IOException) { asyncHandshakeResult.SetComplete(new EndOfStreamException("Connection closed unexpectedly")); } catch (Exception e) { ProcessSendFatalAlert(new Alert(AlertDescription.InternalError, _handshakeSession.NegotiatedVersion)); asyncHandshakeResult.SetComplete(new Exception("Connection closed because of local error", e)); } }