private void EndConnect(IAsyncResult ar)
{
object[] stateObj = (object[])ar.AsyncState;
TcpClient tcpClient = (TcpClient)stateObj[0];
IPEndPoint dstEndPoint = (IPEndPoint)stateObj[1];
byte[] buffer = (byte[])stateObj[2];
string serverCN = (string)stateObj[3];
try
{
m_connectingSockets.Remove(dstEndPoint.ToString());
tcpClient.EndConnect(ar);
SslStream sslStream = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
//DisplayCertificateInformation(sslStream);
SIPConnection callerConnection = new SIPConnection(this, tcpClient, sslStream, dstEndPoint, SIPProtocolsEnum.tls, SIPConnectionsEnum.Caller);
sslStream.BeginAuthenticateAsClient(serverCN, EndAuthenticateAsClient, new object[] { tcpClient, dstEndPoint, buffer, callerConnection });
//sslStream.AuthenticateAsClient(serverCN);
//if (tcpClient != null && tcpClient.Connected)
//{
// SIPConnection callerConnection = new SIPConnection(this, sslStream, dstEndPoint, SIPProtocolsEnum.tls, SIPConnectionsEnum.Caller);
// m_connectedSockets.Add(dstEndPoint.ToString(), callerConnection);
// callerConnection.SIPSocketDisconnected += SIPTLSSocketDisconnected;
// callerConnection.SIPMessageReceived += SIPTLSMessageReceived;
// //byte[] receiveBuffer = new byte[MaxSIPTCPMessageSize];
// callerConnection.SIPStream.BeginRead(callerConnection.SocketBuffer, 0, MaxSIPTCPMessageSize, new AsyncCallback(ReceiveCallback), callerConnection);
// logger.Debug("Established TLS connection to " + dstEndPoint + ".");
// callerConnection.SIPStream.BeginWrite(buffer, 0, buffer.Length, EndSend, callerConnection);
//}
//else
//{
// logger.Warn("Could not establish TLS connection to " + dstEndPoint + ".");
//}
}
catch (Exception excp)
{
logger.Error("Exception SIPTLSChannel EndConnect. " + excp);
if(tcpClient != null)
{
try
{
tcpClient.Close();
}
catch(Exception closeExcp)
{
logger.Warn("Exception SIPTLSChannel EndConnect Close TCP Client. " + closeExcp);
}
}
}
}