/// <summary> /// runs the receiving end /// </summary> /// <param name="token"></param> /// <param name="reader"></param> private void Run(CancellationToken token, TextReader reader) { try { while (!token.IsCancellationRequested) { var line = reader.ReadLine(); if (line == null) { break; } // ReSharper disable once MethodSupportsCancellation (unncessary) Task.Run(() => client.OnRawReceived(line)); } } catch (IOException e) { if ((e.InnerException as SocketException)?.ErrorCode != 10004) { client.OnException(new ExceptionEventArgs(e)); } } catch (Exception e) { client.OnException(new ExceptionEventArgs(e)); } finally { if (!token.IsCancellationRequested) { // ReSharper disable once MethodSupportsCancellation Task.ContinueWith(t => client.HandleUnexpectedDisconnect()); } } }