SSH Tunnel closes

Jun 28, 2015 at 10:35 PM
Hello!
I am trying to make a SSH Tunnel application. I successfully open a tunnel with dynamic port and it works until the first connection by browser is made. After one page is loaded, the tunnel is not working anymore and I unable to open next page until I restart a tunnel. Sometimes there is an exception "Object reference not set to an instance of the object." "System.NullReferenceException" in Renci.SshNet.dll.
My code is following:
        SshClient client;
        ForwardedPortDynamic port;

        public void Start(string server, string user, string password, int serverport=22)
        {
                client = new SshClient(server, user, password);
                client.KeepAliveInterval = new TimeSpan(0, 0, 5);
                client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20);
                client.Connect();

                if (client.IsConnected)
                {
                    try
                    {
                        port = new ForwardedPortDynamic("127.0.0.1", 1080);
                        client.AddForwardedPort(port);
                        port.Start();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }

        }

        public void Stop()
        {
            port.Stop();
            port.Dispose();
            client.Disconnect();
            client.Dispose();
        }
What's wrong with it? Could you please help me make my app work! Many thanks in advance!