private static void ServerThreadStart(object o)
        {
            TcpClient client = o as TcpClient;

            SslStream sslStream = new SslStream(client.GetStream(),
                                                false, //leave inner stream open
                                                new RemoteCertificateValidationCallback(CertificateValidationCallback)
                                                );

            bool authenticationPassed = true;

            try
            {
                X509Certificate cert = GetServerCert(SERVER_CERT_FILENAME, SERVER_CERT_PASSWORD);
                sslStream.AuthenticateAsServer(
                    cert,
                    true,   //client cert required
                    SslProtocols.Default,
                    false); //check for revoked cert
            }
            catch (AuthenticationException)
            {
                authenticationPassed = false;
            }
            if (authenticationPassed)
            {
                FieldIdentifier Code          = new FieldIdentifier("Incoming_" + new FieldGuid().ToString().Replace("-", "_"));
                NodeConnection  newConnection = NodeConnection.BuildWith(Code);
                OnNewIncomingConnection(newConnection);
                newConnection.ThreadStart(sslStream); //essentially blocks until the connection dies
            }
        }
        public static NodeConnection StartOutgoingConnection(FieldIdentifier Code, string hostName, int port)
        {
            if (hostName == null)
            {
                throw new ArgumentNullException("hostName");
            }
            if (port <= 0)
            {
                throw new ArgumentOutOfRangeException("port");
            }

            TcpClient client = new TcpClient();

            client.Connect(hostName, port);

            SslStream sslStream = new SslStream(client.GetStream(),
                                                false,
                                                new RemoteCertificateValidationCallback(CertificateValidationCallback),
                                                new LocalCertificateSelectionCallback(CertificateSelectionCallback));

            bool authenticationPassed = true;

            try
            {
                string serverName = System.Environment.MachineName;

                X509Certificate           cert  = GetServerCert(SERVER_CERT_FILENAME, SERVER_CERT_PASSWORD);
                X509CertificateCollection certs = new X509CertificateCollection();
                certs.Add(cert);

                sslStream.AuthenticateAsClient(
                    serverName,
                    certs,
                    SslProtocols.Default,
                    false); // check cert revokation
            }
            catch (AuthenticationException)
            {
                authenticationPassed = false;
            }
            if (authenticationPassed)
            {
                NodeConnection newConnection = NodeConnection.BuildWith(Code);
                ThreadPool.QueueUserWorkItem(new WaitCallback(newConnection.ThreadStart), sslStream);

                return(newConnection);
            }
            else
            {
                return(null);
            }
        }