public void ConnectSecure(string hostname, int port, bool useEvents = true, bool validateServerCertificate = true)
{
if (Client.Connected)
{
throw new InvalidOperationException("Unable to connect: client is already connected");
}
Client.Connect(hostname, port);
if (OnLocalPortKnown != null)
{
Task.Run(() => OnLocalPortKnown(((IPEndPoint)Client.Client.LocalEndPoint).Port));
}
var sslStream = new SslStream(Client.GetStream(), false, validateServerCertificate ? new RemoteCertificateValidationCallback(ValidateServerCertificate) : null, null);
sslStream.AuthenticateAsClient(hostname);
Log(string.Format("SSL Connection established: Cipher: {0}-bit {1}; KEX: {2} {3}-bit; Hash: {4} {5}-bit",
sslStream.CipherStrength, sslStream.CipherAlgorithm,
sslStream.KeyExchangeAlgorithm, sslStream.KeyExchangeStrength,
sslStream.HashAlgorithm, sslStream.HashStrength));
ConnectionStream = sslStream;
readBuffer = new byte[Client.ReceiveBufferSize];
if (useEvents)
{
EnterReadLoop();
sslStream.BeginRead(readBuffer, 0, readBuffer.Length, ReadCallback, Client);
}
}