|  | @@ -71,63 +71,54 @@ namespace Renci.SshNet.Channels
 | 
	
		
			
				|  |  |                  return;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              //  Start reading data from the port and send to channel
 | 
	
		
			
				|  |  | -            var readerTaskCompleted = new ManualResetEvent(false);
 | 
	
		
			
				|  |  |              Exception exception = null;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            this.ExecuteThread(() =>
 | 
	
		
			
				|  |  | +            try
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  | -                try
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    var buffer = new byte[this.PacketSize - 9];
 | 
	
		
			
				|  |  | +                var buffer = new byte[this.PacketSize - 9];
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                    while (this._socket.Connected || this.IsConnected)
 | 
	
		
			
				|  |  | +                while (this._socket != null && this._socket.Connected && this._socket.Poll(1, SelectMode.SelectRead) && this._socket.Available > 0)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    try
 | 
	
		
			
				|  |  |                      {
 | 
	
		
			
				|  |  | -                        try
 | 
	
		
			
				|  |  | +                        var read = 0;
 | 
	
		
			
				|  |  | +                        this.InternalSocketReceive(buffer, ref read);
 | 
	
		
			
				|  |  | +                        if (read > 0)
 | 
	
		
			
				|  |  |                          {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                            var read = 0;
 | 
	
		
			
				|  |  | -                            this.InternalSocketReceive(buffer, ref read);
 | 
	
		
			
				|  |  | -                            if (read > 0)
 | 
	
		
			
				|  |  | -                            {
 | 
	
		
			
				|  |  | -                                this.SendMessage(new ChannelDataMessage(this.RemoteChannelNumber, buffer.Take(read).ToArray()));
 | 
	
		
			
				|  |  | -                            }
 | 
	
		
			
				|  |  | -                            else
 | 
	
		
			
				|  |  | -                            {
 | 
	
		
			
				|  |  | -                                break;
 | 
	
		
			
				|  |  | -                            }
 | 
	
		
			
				|  |  | +                            this.SendMessage(new ChannelDataMessage(this.RemoteChannelNumber, buffer.Take(read).ToArray()));
 | 
	
		
			
				|  |  |                          }
 | 
	
		
			
				|  |  | -                        catch (SocketException exp)
 | 
	
		
			
				|  |  | +                        else
 | 
	
		
			
				|  |  |                          {
 | 
	
		
			
				|  |  | -                            if (exp.SocketErrorCode == SocketError.WouldBlock ||
 | 
	
		
			
				|  |  | -                                exp.SocketErrorCode == SocketError.IOPending ||
 | 
	
		
			
				|  |  | -                                exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
 | 
	
		
			
				|  |  | -                            {
 | 
	
		
			
				|  |  | -                                // socket buffer is probably empty, wait and try again
 | 
	
		
			
				|  |  | -                                Thread.Sleep(30);
 | 
	
		
			
				|  |  | -                            }
 | 
	
		
			
				|  |  | -                            else if (exp.SocketErrorCode == SocketError.ConnectionAborted || exp.SocketErrorCode == SocketError.ConnectionReset)
 | 
	
		
			
				|  |  | -                            {
 | 
	
		
			
				|  |  | -                                break;
 | 
	
		
			
				|  |  | -                            }
 | 
	
		
			
				|  |  | -                            else
 | 
	
		
			
				|  |  | -                                throw;  // throw any other error
 | 
	
		
			
				|  |  | +                            break;
 | 
	
		
			
				|  |  |                          }
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  | +                    catch (SocketException exp)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        if (exp.SocketErrorCode == SocketError.WouldBlock ||
 | 
	
		
			
				|  |  | +                            exp.SocketErrorCode == SocketError.IOPending ||
 | 
	
		
			
				|  |  | +                            exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            // socket buffer is probably empty, wait and try again
 | 
	
		
			
				|  |  | +                            Thread.Sleep(30);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        else if (exp.SocketErrorCode == SocketError.ConnectionAborted || exp.SocketErrorCode == SocketError.ConnectionReset)
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            break;
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        else
 | 
	
		
			
				|  |  | +                            throw;  // throw any other error
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -                catch (Exception exp)
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    exception = exp;
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                finally
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    readerTaskCompleted.Set();
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            });
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            catch (Exception exp)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                exception = exp;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              //  Channel was open and we MUST receive EOF notification, 
 | 
	
		
			
				|  |  |              //  data transfer can take longer then connection specified timeout
 | 
	
		
			
				|  |  |              //  If listener thread is finished then socket was closed
 | 
	
		
			
				|  |  | -            System.Threading.WaitHandle.WaitAny(new WaitHandle[] { this._channelEof, readerTaskCompleted });
 | 
	
		
			
				|  |  | +            System.Threading.WaitHandle.WaitAny(new WaitHandle[] { this._channelEof });
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              //  Close socket if still open
 | 
	
		
			
				|  |  |              if (this._socket != null)
 |