title: Request Timeout for pushing notifications using GCM , works Intermittently for the first 2 -3 times. But fails many times
body: <p>I have created a Push Notifications for Android using GCM.<br/>
  The Service is facing many timeouts and it works intermittently.
  I have a Windows service which runs at an arbitary time(30 Secs in this case.) and checks the Message Queue , and if any messages existing it sends it to the respective device.
  <br/>
  The Pattern is : <br/>
  It works for first two three times and if
  Once it fails , it starts to fail continuously.</p>
  
  <p>What could be the reason for failure ?</p>
  
  <p><strong>EDIT</strong>
  I thought this may be occuring due to organisation's firewall but that also can be over ruled since i have tried it on a open network.
  <em>Any inputs any thinking lines would be helpful.</em>
  <strong><em>THANKS in Advance</em></strong> </p>
  
  <p>Code :</p>
  
  <pre><code>private void SendAndroidMessage(string deviceID,string Message ,DeviceDataModel ddm)
          {
  
           StringBuilder postMsgBuilder = new StringBuilder();
              postMsgBuilder.Append("registration_id=").Append(deviceID);
              //postMsgBuilder.Append("&amp;").Append("delay_while_idle=").Append("false");
              postMsgBuilder.Append("&amp;").Append("data.message=").Append(Message);
  
              byte[] messageBytes = Encoding.UTF8.GetBytes(postMsgBuilder.ToString());
  
              var request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
  
              // Hook a callback to verify the remote certificate
              ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(C2dmCertValidationCallback);
              request.Method = WebRequestMethods.Http.Post;
              request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
              request.ContentLength = messageBytes.Length;
              request.Headers.Add(HttpRequestHeader.Authorization, "key=" + AuthorisationKey);
  
              bool sent = false;
              int tries = 0;
              HttpWebResponse response;
              while (!sent &amp;&amp; tries &lt; this.numOfRetries)
              {
                  try
                  {
                      using (var requestStream = request.GetRequestStream())
                      {
                          // we are not using retries yet.. or looking for errors here. We should log errors too.
                          requestStream.Write(messageBytes, 0, messageBytes.Length);
                      }
  
                      // Sends the notification and gets the response.
                      response = (HttpWebResponse)request.GetResponse();
                      if (response.StatusCode == HttpStatusCode.NotFound)
                      {
                          return;
                      }
  
                      if (response.StatusCode != HttpStatusCode.OK)
                      {
                          tries++;
                          if (tries &gt; this.numOfRetries)
                          {
                              if (this.NotificationFailed != null)
                              {
                                  this.NotificationFailed(this, new NotificationEventArgs(new NotificationException(string.Format(CultureInfo.InvariantCulture, "{0} notification failures for {1} for DeviceId:{2} of DeviceType:{3}. Giving up", this.numOfRetries, C2dmUrlStr,ddm.DeviceId,ddm.DeviceType))));
                              }
  
                              return;
                          }
                      }
                      else
                      {
                          sent = true;
                      }
                  }
                  catch (WebException e)
                  {
                      // check why we got the exception 
                      string responseString = "";
                      if (e.Response != null)
                      {
                          StreamReader reader = new StreamReader(e.Response.GetResponseStream());
                         responseString= reader.ReadToEnd();
                      }
                      if (this.NotificationFailed != null)
                      {
                          this.NotificationFailed(this, new NotificationEventArgs(new NotificationException("Notification failed with exception " + e.Message + "for " + "https://android.googleapis.com/gcm/send"+" DeviceId: "+ddm.DeviceId+" Device Type: "+ddm.DeviceType+"Response String : "+ responseString, e)));
                      }
  
                      throw;
                  }
              }
          }
  </code></pre>