|  | @@ -3,6 +3,7 @@ using System.Collections.Generic;
 | 
	
		
			
				|  |  |  using System.Net;
 | 
	
		
			
				|  |  |  using System.Net.Sockets;
 | 
	
		
			
				|  |  |  using System.Text;
 | 
	
		
			
				|  |  | +using System.Threading;
 | 
	
		
			
				|  |  |  using Microsoft.VisualStudio.TestTools.UnitTesting;
 | 
	
		
			
				|  |  |  using Moq;
 | 
	
		
			
				|  |  |  using Renci.SshNet.Channels;
 | 
	
	
		
			
				|  | @@ -20,6 +21,7 @@ namespace Renci.SshNet.Tests.Classes
 | 
	
		
			
				|  |  |          private Socket _client;
 | 
	
		
			
				|  |  |          private IList<EventArgs> _closingRegister;
 | 
	
		
			
				|  |  |          private IList<ExceptionEventArgs> _exceptionRegister;
 | 
	
		
			
				|  |  | +        private ManualResetEvent _exceptionFired;
 | 
	
		
			
				|  |  |          private TimeSpan _connectionTimeout;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          [TestInitialize]
 | 
	
	
		
			
				|  | @@ -38,17 +40,25 @@ namespace Renci.SshNet.Tests.Classes
 | 
	
		
			
				|  |  |                  _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(5));
 | 
	
		
			
				|  |  |                  _forwardedPort.Stop();
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              if (_client != null)
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  |                  _client.Close();
 | 
	
		
			
				|  |  |                  _client = null;
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (_exceptionFired != null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                _exceptionFired.Dispose();
 | 
	
		
			
				|  |  | +                _exceptionFired = null;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          private void SetupData()
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              _closingRegister = new List<EventArgs>();
 | 
	
		
			
				|  |  |              _exceptionRegister = new List<ExceptionEventArgs>();
 | 
	
		
			
				|  |  | +            _exceptionFired = new ManualResetEvent(false);
 | 
	
		
			
				|  |  |              _connectionTimeout = TimeSpan.FromSeconds(5);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -70,6 +80,7 @@ namespace Renci.SshNet.Tests.Classes
 | 
	
		
			
				|  |  |              _channelMock.InSequence(seq).Setup(p => p.Close());
 | 
	
		
			
				|  |  |              _channelMock.InSequence(seq).Setup(p => p.Dispose());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          private void Arrange()
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              SetupData();
 | 
	
	
		
			
				|  | @@ -78,7 +89,11 @@ namespace Renci.SshNet.Tests.Classes
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              _forwardedPort = new ForwardedPortDynamic(8122);
 | 
	
		
			
				|  |  |              _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
 | 
	
		
			
				|  |  | -            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
 | 
	
		
			
				|  |  | +            _forwardedPort.Exception += (sender, args) =>
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    _exceptionRegister.Add(args);
 | 
	
		
			
				|  |  | +                    _exceptionFired.Set();
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  |              _forwardedPort.Session = _sessionMock.Object;
 | 
	
		
			
				|  |  |              _forwardedPort.Start();
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -92,6 +107,10 @@ namespace Renci.SshNet.Tests.Classes
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              var buffer = new byte[] {0x07};
 | 
	
		
			
				|  |  |              _client.Send(buffer, 0, buffer.Length, SocketFlags.None);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            // wait for Exception event to be fired as a way to ensure that SOCKS
 | 
	
		
			
				|  |  | +            // request has been handled completely
 | 
	
		
			
				|  |  | +            _exceptionFired.WaitOne(TimeSpan.FromMilliseconds(200));
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          [TestMethod]
 |