title: .NET IRC TCPClient read hang after 30-40 minutes
body: <p>I've been trying to write an IRC bot in C# using a TCPClient. It connects and starts receiving commands, responding to the PING command with a PONG command. That's all.</p>
  
  <p>However, for some reason it hangs after 30 to 40 minutes on the function to read the next line of data. I've inspected the PING and PONG messages using Wireshark and they look fine to me. The PONGs are also received by the server as I do see an ACK packet being received by my computer in Wireshark.</p>
  
  <p>The weird thing is that it works absolutely fine for those 30 to 40 minutes.
  I suspect I'm doing something wrong with the StreamReader but after searching the web for a few days I'm stuck.</p>
  
  <p>Would somebody be so kind as to look at my code? Thanks a lot in advance.</p>
  
  <pre><code>public class Bot
  {
      private NetworkStream ns;
      private StreamReader reader;
      private StreamWriter writer;
      private Encoding enc;           // The encoding used.
  
      /// &lt;summary&gt;
      /// Initialize the bot.
      /// &lt;/summary&gt;
      public Bot()
      {
          enc = new UTF8Encoding();
      }
  
      /// &lt;summary&gt;
      /// Connects the an IRC server.
      /// &lt;/summary&gt;
      /// &lt;param name="url"&gt;The url of the server.&lt;/param&gt;
      /// &lt;param name="port"&gt;The port to connect to.&lt;/param&gt;
      /// &lt;param name="user"&gt;The username to use.&lt;/param&gt;
      /// &lt;param name="nick"&gt;The nick to use.&lt;/param&gt;
      /// &lt;param name="realName"&gt;The users real name.&lt;/param&gt;
      public void Connect(string url, ushort port, string user, string nick, string realName)
      {
          TcpClient client = new TcpClient();
  
          try
          {
              client.Connect(url, port);
          }
          catch (Exception ex)
          {
              throw new Exception("Could not connect to endpoint.", ex);
          }
  
          ns = client.GetStream();
          reader = new StreamReader(ns, enc);
          writer = new StreamWriter(ns, enc);
          writer.AutoFlush = true;
  
          Send("USER " + user + " 0 * :" + realName + "\r\n");
          Send("NICK " + nick + "\r\n");
      }
  
      /// &lt;summary&gt;
      /// Processes a command.
      /// &lt;/summary&gt;
      /// &lt;param name="command"&gt;The command to process.&lt;/param&gt;
      public virtual void ProcessCommand(IRCCommand command)
      {
          if(command.Command == "PING")
          {
              Send("PONG :" + command.Parameters[0] + "\r\n");
          }
      }
  
      /// &lt;summary&gt;
      /// Receives and processes a command.
      /// &lt;/summary&gt;
      public void ReceiveAndProcess()
      {
          string line = reader.ReadLine();
  
          if (!string.IsNullOrEmpty(line))
          {
              Console.WriteLine("raw : " + line);
  
              IRCCommand cmd = new IRCCommand();
              cmd.Parse(line);
  
              ProcessCommand(cmd);
          }
      }
  
      /// &lt;summary&gt;
      /// Sends a command to the irc server.
      /// &lt;/summary&gt;
      /// &lt;param name="ircCommand"&gt;The command to send.&lt;/param&gt;
      protected void Send(string ircCommand)
      {
          Console.Write("sent: " + ircCommand);
          writer.Write(ircCommand);
      }
  }
  </code></pre>