title: GZipStream, how correctly read from GZipStream
body: <p>I have some code writen by me in C#</p>
  
  <pre><code>        string host = new Uri(_url).Host;
          IPHostEntry ipAddress = Dns.GetHostEntry(host);
          IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80);
          using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
          {
              s.Connect(ip);
              using (NetworkStream n = new NetworkStream(s))
              {
                      byte[] write = encoding.GetBytes(HttpQuery);
                      n.Write(write, 0, write.Length);
                      ReadLine(n);
                      Dictionary&lt;string, string&gt; headers = new Dictionary&lt;string, string&gt;();
                      while (true)
                      {
                          string line = ReadLine(n);
                          if (line.Length == 0)
                          {
                              break;
                          }
                          int index = line.IndexOf(':');
                          if (!headers.ContainsKey(line.Substring(0, index)))
                          {
                              headers.Add(line.Substring(0, index), line.Substring(index + 2));
                          }
                      }
  
                      string contentEncoding;
                      if (headers.TryGetValue("Content-Encoding", out contentEncoding))
                      {
                          Stream responseStream = n;
                          if (contentEncoding.Equals("gzip"))
                          {
                              responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                              responseStream.Flush();
                          }
                          else if (contentEncoding.Equals("deflate"))
                          {
                              responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
                          }
  
                          MemoryStream memStream = new MemoryStream();
  
                          byte[] respBuffer = new byte[4096];
                          try
                          {
                              int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                              //int bytesRead = responseStream.ReadByte();
                              while (bytesRead &gt; 0)
                              {
                                  memStream.Write(respBuffer, 0, bytesRead);
                                  bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                              }
                          }
                          finally
                          {
                              responseStream.Close();
                          }
                          string str = encoding.GetString(memStream.ToArray());
  </code></pre>
  
  <p>Then I Have an exception InvalidDataException in this line <strong>int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);</strong></p>
  
  <p>GZip header magic number is not correct.</p>
  
  <pre><code>    string ReadLine(Stream stream)
      {
          List&lt;byte&gt; lineBuffer = new List&lt;byte&gt;();
          try
          {
              while (true)
              {
                  int b = stream.ReadByte();
                  if (b == -1)
                      return null;
                  if (b == 10)
                      break;
                  if (b != 13)
                      lineBuffer.Add((byte) b);
              }
          }
          catch (Exception)
          {
          }
          return encoding.GetString(lineBuffer.ToArray());
      }
  </code></pre>
  
  <p>Any ideas?</p>