title: Terminating socket connection when receiving newline
body: <p>I am trying to create a mechanism where my server stops reading input after it encounters only a newline.</p>
  
  <p>Here is the code (most of it taken from MSDN examples):</p>
  
  <pre><code>private void ReadRequest(IAsyncResult ar)
  {
      SocketState state = (SocketState) ar.AsyncState;
      Socket handler = state.workSocket;
  
      try
      {
          int read = handler.EndReceive(ar);
          if (read &gt; 0)
          {
              string line = Encoding.ASCII.GetString(state.buffer, 0, read);
              byte[] temp = Encoding.ASCII.GetBytes(line);
              string hex = BitConverter.ToString(temp);
              eventLog.WriteEntry(string.Format("Bytes read: {0}, Content: {1}, Hex: {2}", line.Length, line, hex));
              if (line.Equals(Environment.NewLine) || line.Equals('\n'))
              {
                  eventLog.WriteEntry("Double line break.");
                  string response = HandleRequest(state.sb.ToString());
                  handler.BeginSend(Encoding.ASCII.GetBytes(response), 0, response.Length, SocketFlags.None, new AsyncCallback(SendDone), state);
                  return;
              }
  
              state.sb.Append(line);
              handler.BeginReceive(state.buffer, 0, SocketState.BufferSize, SocketFlags.None, new AsyncCallback(ReadRequest), state);
          }
          else
          {
              string response = HandleRequest(state.sb.ToString());
              eventLog.WriteEntry(response, EventLogEntryType.Information);
              handler.BeginSend(Encoding.ASCII.GetBytes(response), 0, response.Length, SocketFlags.None, new AsyncCallback(SendDone), state);
          }
      }
      catch (Exception exception)
      {
          eventLog.WriteEntry(string.Format("Error occured while reading the request: {0}", exception.Message), EventLogEntryType.Error);
      }
  }
  </code></pre>
  
  <p>But the newline condition is never true and so the server never stops reading input. </p>
  
  <p>I print out the hex characters of what I send and when I connect using netcat:</p>
  
  <pre><code>C:\WINDOWS&gt; nc localhost 55432
  Foo&lt;newline&gt;
  &lt;newline&gt;
  </code></pre>
  
  <p>Sure enough, the last line that is sent to the server is:</p>
  
  <pre><code>Bytes read: 1, Content: 
  , Hex: 0A
  </code></pre>
  
  <p>I've checked and <code>0A</code> is hex for <code>\n</code>, so why does my check not work?</p>