private void StartWebRequest(bool retry, string content)
        {
            lock (this)
            {
                webRequestId++;
            }

            waitingRequests++;

            lastSend = DateTime.Now;

            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(Address);

            WebRequestState state = new WebRequestState(req);
            state.Started = DateTime.Now;
            state.WebRequestId = webRequestId;

            if (!retry)
                state.Output = BuildPostData();
            else
                state.Output = content;

            req.Method          = METHOD;
            req.ContentType     = CONTENT_TYPE;
            req.Timeout         = m_Wait * 1000;
            req.KeepAlive       = m_KeepAlive;
            req.ContentLength   = state.Output.Length;

            // Create the delegate that invokes methods for the timer.
            TimerCallback timerDelegate = new TimerCallback(TimeOutGetRequestStream);
            Timer timeoutTimer = new Timer(timerDelegate, state, WEBREQUEST_TIMEOUT, WEBREQUEST_TIMEOUT);
            state.TimeOutTimer = timeoutTimer;

            //Console.WriteLine(String.Format("Start Webrequest: id:{0}", webRequestId.ToString()));
            try
            {
                IAsyncResult result = req.BeginGetRequestStream(new AsyncCallback(this.OnGetRequestStream), state);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void RequestBoshSession()
        {
            /*
            Example 1. Requesting a BOSH session

            POST /webclient HTTP/1.1
            Host: httpcm.jabber.org
            Accept-Encoding: gzip, deflate
            Content-Type: text/xml; charset=utf-8
            Content-Length: 104

            <body content='text/xml; charset=utf-8'
                  hold='1'
                  rid='1573741820'
                  to='jabber.org'
                  route='xmpp:jabber.org:9999'
                  secure='true'
                  ver='1.6'
                  wait='60'
                  ack='1'
                  xml:lang='en'
                  xmlns='http://jabber.org/protocol/httpbind'/>
             */

            lastSend = DateTime.Now;

            // Generate the keys
            GenerateKeys();
            rid = GenerateRid();
            Body body = new Body();
            /*
             * <body hold='1' xmlns='http://jabber.org/protocol/httpbind'
             *  to='vm-2k'
             *  wait='300'
             *  rid='782052'
             *  newkey='8e7d6cec12004e2bfcf7fc000310fda87bc8337c'
             *  ver='1.6'
             *  xmpp:xmlns='urn:xmpp:xbosh'
             *  xmpp:version='1.0'/>
             */

            body.Version        = BOSH_VERSION;
            body.XmppVersion    = "1.0";
            body.Hold           = m_Hold;
            body.Wait           = m_Wait;
            body.Rid            = rid;
            body.Polling        = 0;
            body.Requests       = m_Requests;
            body.To             = new Jid(m_XmppCon.Server);

            body.NewKey         = Keys[CurrentKeyIdx];

            body.SetAttribute("xmpp:xmlns", "urn:xmpp:xbosh");

            waitingRequests++;

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Address);

            WebRequestState state = new WebRequestState(req);
            state.Started           = DateTime.Now;
            state.Output            = body.ToString();
            state.IsSessionRequest  = true;

            req.Method          = METHOD;
            req.ContentType     = CONTENT_TYPE;
            req.Timeout         = m_Wait * 1000;
            req.KeepAlive       = m_KeepAlive;
            req.ContentLength   = state.Output.Length;

            try
            {
                IAsyncResult result = req.BeginGetRequestStream(new AsyncCallback(this.OnGetSessionRequestStream), state);
            }
            catch (Exception)
            {
            }
        }