title: When the HTTP headers are already sent?
body: <p>I have this module:</p>
  
  <pre><code>public class SecureCookieModule : IHttpModule
  {
      public void Dispose()
      {
      }
  
      public void Init(HttpApplication context)
      {
          context.EndRequest += OnEndRequestHandlerExecute;
      }
  
      private void OnEndRequestHandlerExecute(object sender, EventArgs e)
      {
          if (!AppConfig.MachineAppSettingsBool("setSecureCookiesForSecureConnections"))
          {
              return;
          }
  
          try
          {
              HttpApplication app = sender as HttpApplication;
  
              if (app.Request.IsReallySecureConnection())
              {
                  foreach (string s in app.Response.Cookies.AllKeys)
                  {
                      var cookie = app.Response.Cookies[s];
  
                      if (cookie.Secure == false)
                      {
                          cookie.Secure = true;
                          app.Response.Cookies.Add(cookie);
                      }
                  }
              }
          }
          catch (Exception ex)
          {
              LogManager.GetLogger("SecureCookieModule").Error("Exception while processing http module. Ex: {0}", ex);
          }
      }
  }
  </code></pre>
  
  <p>From this what I wrote in some cases the response headers are already sent, so they cannot be modified and exception is thrown because of this. What I miss is in which cases this happens and why? Why in some cases before executing the "OnEndRequestHandlerExecute" event they are sent and in other cases they are not? What is the rule behind this cases resulting in this behavior? </p>
  
  <p>Thanks in advance!</p>