title: Not getting response from SslStream.read
body: <p>I'm trying to do a simple Gmail client.  The problem is that I'm getting a pretty weird response; actually I'm not getting response from <code>SslStream.read</code> at all.  First when I connect to the gmail server as client, it works perfectly (<code>Connect()</code> method), but then when I'm trying to get the number off emails, again using <code>SslStream.read</code> with <code>leerMensaje()</code> method, it gets weird and just stops running, and the webpage gets stuck loading.</p>
  
  <p>Here is the class I'm working on:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Net;
  using System.Net.Sockets;
  using System.Net.Security;
  using System.Text;
  using System.Net.Mail;
  
  namespace GmailClient
  {
      public class GmailPop
      {
          private SslStream sslStream;
          private String user;
          private String pass;
  
          public GmailPop(String user, String pass)
          {
              this.user = user;
              this.pass = pass;
          }
  
          public bool Connect()
          {
              String response;
              byte[] buffer = new byte[2048];
              int bytes = -1;
              TcpClient server = new TcpClient("pop.gmail.com", 995);
              sslStream = new SslStream(server.GetStream());
              sslStream.AuthenticateAsClient("pop.gmail.com");
              bytes = sslStream.Read(buffer, 0, buffer.Length);
              response = Encoding.ASCII.GetString(buffer,0,bytes);
              sslStream.Write(Encoding.ASCII.GetBytes("USER recent:" + user+"\r\n"));
              bytes = sslStream.Read(buffer, 0, buffer.Length);
              response = Encoding.ASCII.GetString(buffer,0,bytes);
              if(response.StartsWith("+OK")){
                  sslStream.Write(Encoding.ASCII.GetBytes("PASS "+pass+"\r\n"));
                  bytes = sslStream.Read(buffer,0,buffer.Length);
                  response = Encoding.ASCII.GetString(buffer, 0, bytes);
                  return response.StartsWith("+OK");
              }
              return false;
          }
          public int numCorreos()
          {
              String res = escribirLeer("STAT");
              String[] resA = res.Split(' ');
              int num = Int32.Parse(resA[1]);
              return num;
          }
          public String leerMensaje()
          {
              int bytes = -1;
              byte[] buffer = new byte[2048];
              String msg = "";
              do
              {
                  bytes = sslStream.Read(buffer, 0, buffer.Length);
                  msg += Encoding.ASCII.GetString(buffer, 0, bytes);
              } while (bytes != 0);
              return msg;
          }
          public void escribirMsg(String msg)
          {
              byte[] buffer = Encoding.ASCII.GetBytes(msg);
              sslStream.Write(buffer);
              sslStream.Flush();
          }
          public String escribirLeer(String msg)
          {
              escribirMsg(msg);
              String respuesta = leerMensaje();
              return respuesta;
          }
      }
  }
  </code></pre>
  
  <p>And here is the aspx webpage that calls the method:</p>
  
  <pre><code>GmailPop client;
          protected void Page_Load(object sender, EventArgs e)
          {
              if (Session["user"] != null &amp;&amp; Session["pass"] != null)
              {
                  String user = Session["user"].ToString();
                  String pass = Session["pass"].ToString();
  
                  client = new GmailPop(user, pass);
  
                  if (client.Connect())
                  {                    
                      Session["active"] = true;
                      Response.Write(numCorreos());
                  }
                  else
                  {
                      Session.Remove("pass");
                      Response.Redirect("login.aspx?error=login");
                  }
              }
              else
              {
                  Response.Redirect("login.aspx");
              }
          }
      }
  </code></pre>