internal void Connect()
        {
            if (m_Connected)
            {
                throw new ErlException(StringConsts.ERL_CONN_ALREADY_CONNECTED_ERROR);
            }

            if (m_Transport != null)
            {
                m_Transport.Trace += (o, t, d, msg) => m_Home.OnTrace(t, d, msg);
            }

            // now get a connection between the two...
            int port = 0;

            using (var ps = ErlTransportPasswordSource.StartPasswordSession(m_Peer.NodeName, m_Peer.SSHUserName))
            {
                // now get a connection between the two...
                port = ErlEpmd.LookupPort(LocalNode, m_Peer, true);

                if (port == 0)
                {
                    throw new ErlException(StringConsts.ERL_EPMD_INVALID_PORT_ERROR.Args(m_Peer.NodeName));
                }

                doConnect(port);
            }

            m_Peer.Port = port;

            m_Connected = true;
        }
 public void Dispose()
 {
     ErlTransportPasswordSource.FinishPasswordSession(this);
     if (Password != null)
     {
         Password.Dispose(); //dispose Password
     }
 }
        /// <summary>
        /// Connect to remote host:port over SSH tunnel
        /// </summary>
        public void Connect(string host, int port)
        {
            try
            {
                //remember remote target
                m_RemoteTarget = new IPEndPoint(ResolveHost(host), port);

                //connect to SSH server
                m_Client.Connect(new IPEndPoint(m_RemoteTarget.Address, SSHServerPort));

                //get password from user
                var pass = ErlTransportPasswordSource.GetPassword(this, NodeName, SSHUserName);

                //set params
                var param = new SSHConnectionParameter();
                param.EventTracer        = this; //to receive detailed events
                param.UserName           = SSHUserName;
                param.Password           = pass;
                param.Protocol           = SSHProtocol.SSH2;
                param.AuthenticationType = (AuthenticationType)Enum.Parse(typeof(SSH.AuthenticationType), SSHAuthenticationType);

                if (param.AuthenticationType == AuthenticationType.PublicKey)
                {
                    param.IdentityFile = SSHPrivateKeyFilePath;
                }

                //former algorithm is given priority in the algorithm negotiation
                param.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA };
                param.PreferableCipherAlgorithms  = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES, CipherAlgorithm.AES192CTR, CipherAlgorithm.AES256CTR, CipherAlgorithm.AES128CTR };

                param.WindowSize = 0x1000; //this option is ignored with SSH1

                //Creating a new SSH connection over the underlying socket
                m_Connection = SSHConnection.Connect(param, this, m_Client);
                m_Connection.AutoDisconnect = true;
                m_IsChannelReady            = false;

                //Local->Remote port forwarding (we use localhost:0 as local port, because local port is not required for us, we will use this tunnel directly)
                m_Channel = m_Connection.ForwardPort(this, host, port, "localhost", 0);
                var deadLine = DateTime.Now.AddMilliseconds(SSHTunnelCreationTimeout);
                while (!m_IsChannelReady && deadLine > DateTime.Now)
                {
                    System.Threading.Thread.Sleep(50); //wait response
                }
                //if timeouted - throw exception
                if (!m_IsChannelReady && deadLine < DateTime.Now)
                {
                    throw new ErlException(ERL_CREATE_SSH_TUNNEL_ERROR);
                }

                //create network stream
                m_Stream = new SshTunnelStream(m_Channel);

                //Remote->Local
                // if you want to listen to a port on the SSH server, follow this line:
                //_conn.ListenForwardedPort("0.0.0.0", 10000);

                //NOTE: if you use SSH2, dynamic key exchange feature is supported.
                //((SSH2Connection)_conn).ReexchangeKeys();
            }
            catch (Exception ex)
            {
                OnTrace(ErlTraceLevel.Ctrl, Direction.Inbound, ex.Message);
                throw;
            }
        }