/// <summary>
 /// Connect the client to the EndPoint.
 /// </summary>
 /// <param name="endPoint">The remote end point</param>
 public void Connect(IPEndPoint endPoint)
 {
     // Connect the client to the remote end point
     Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, this.KeepAlive);
     client.Connect(endPoint);
     client.Blocking = true;
     // Create the socket client handler, add the callback for the event and start to receiving
     handler = GetHandler(client, null);
     handler.ReceiveMessageEvent += new ReceiveMessageDelegate(handler_ReceiveMessageEvent);
     handler.CloseConnectionEvent += new SocketConnectionDelegate(handler_CloseConnectionEvent);
     handler.InReceivingEvent += new SocketConnectionDelegate(handler_InReceivingEvent);
     connected = true;
     this.OnConnection(handler);
     handler.StartReceive();
     //((IPEndPoint)client.LocalEndPoint).Port
 }
 /// <summary>
 /// Connect the client to the EndPoint on SSL.
 /// </summary>
 /// <param name="endPoint">The remote end point</param>
 /// <param name="clientCertificatePath">The client certificate file</param>
 /// <param name="certificatePassword">The client certifciate password</param>
 public void Connect(IPEndPoint endPoint, string clientCertificatePath, string certificatePassword)
 {
     // Load the client certificte
     X509Certificate2 clientCertificate = new X509Certificate2(clientCertificatePath, certificatePassword);
     X509CertificateCollection clientCertificateList = new X509CertificateCollection();
     clientCertificateList.Add(clientCertificate);
     // Connect the client to the remote end point
     TcpClient sslTcpClient = new TcpClient();
     sslTcpClient.Connect(endPoint);
     sslTcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, this.KeepAlive);
     // Open a ssl stream for the communication
     SslStream sslStream = new SslStream(sslTcpClient.GetStream(), false, new RemoteCertificateValidationCallback(this.OnVerifyCertificate));
     sslStream.AuthenticateAsClient("NONE", clientCertificateList, SslProtocols.Ssl3, false); //TODO: params from config for mutual auth, protocol and revocation
     // Create the socket client handler, add the callback for the event and start to receiving
     Socket client = sslTcpClient.Client;
     client.Blocking = true;
     handler = GetHandler(client, sslStream);
     handler.ReceiveMessageEvent += new ReceiveMessageDelegate(handler_ReceiveMessageEvent);
     handler.CloseConnectionEvent += new SocketConnectionDelegate(handler_CloseConnectionEvent);
     handler.InReceivingEvent += new SocketConnectionDelegate(handler_InReceivingEvent);
     connected = true;
     this.OnConnection(handler);
     handler.StartReceive();
 }