// Must hold lock first.
        private HttpSocket GetSocket()
        {
            // Debug.Assert(!BothPending);

            // Switch to the other socket than the last one, assuming the other socket isn't pending.
            // If the other socket is pending, use the last one.
            HttpSocket other = (m_lastSock == m_sockA) ? m_sockB : m_sockA;

            if (!other.IsPending)
            {
                m_lastSock = other;
            }

            Debug.WriteLine("Socket: " + m_lastSock.Name);
            return(m_lastSock);
        }
        void ISocketEventListener.OnConnect(BaseSocket sock)
        {
            lock (m_queue)
            {
                if (!m_running &&
                    m_sockA != null && m_sockA.Connected &&
                    m_sockB != null && m_sockB.Connected)
                {
                    m_running  = true;
                    m_lastSock = m_sockB;

                    m_thread = new Thread(ProcessThread);
                    m_thread.IsBackground = true;
                    m_thread.Name         = "XEP 124 processing thread";
                    m_thread.Start();

                    m_listener.OnConnect(this);
                }
            }
        }
        /// <summary>
        /// Start polling
        /// </summary>
        /// <param name="addr">Ignored in this case.  Set URL.</param>
        public override void Connect(Address addr)
        {
            Debug.Assert(m_uri != null);

            m_rid      = -1L;
            m_lastSock = null;
            m_running  = false;

            // Create new ones each time, in case the URL has changed or something.
            m_sockA = new HttpSocket(this);
            m_sockB = new HttpSocket(this);

            m_sockA.Name = "A";
            m_sockB.Name = "B";

            m_sockA.ProxyURI         = m_sockB.ProxyURI = m_proxyURI;
            m_sockA.ProxyCredentials = m_sockB.ProxyCredentials = m_proxyCredentials;

            m_sockA.Connect(m_uri);
            m_sockB.Connect(m_uri);
        }
        /// <summary>
        /// Stop polling.
        /// </summary>
        public override void Close()
        {
            Body body = CreateOpenBodyTag();

            body.Type = BodyType.terminate;

            Enqueue(body);

            if (m_thread != null)
            {
                m_thread.Join();
            }

            lock (m_queue)
            {
                m_running = false;
                m_thread  = null;
                m_sockA   = m_sockB = m_lastSock = null;
            }
            m_listener.OnClose(this);
        }
        public void Test_Full_Response()
        {
            SocketWatcher watcher = new SocketWatcher();
            Address a = new Address("127.0.0.1", 7002);
            a.Resolve();

            ServerListener server = new ServerListener();
            AsyncSocket server_sock = watcher.CreateListenSocket(server, a);
            server_sock.RequestAccept();

            ResponseListener resp = new ResponseListener();
            HttpSocket sock = new HttpSocket(resp);

            Uri u = new Uri("http://localhost:7002/");
            byte[] buf = ENC.GetBytes("11111");
            HttpSocket s = (HttpSocket)sock;
            s.Execute("GET", u, buf, 0, buf.Length, "text/plain");
            resp.Event.WaitOne();
            Assert.AreEqual("1234567890", resp.Last);

            resp.Last = null;
            buf = ENC.GetBytes("22222");
            s.Execute("GET", u, buf, 0, buf.Length, "text/plain");
            resp.Event.WaitOne();
            Assert.AreEqual("1234567890", resp.Last);

            resp.Last = null;
            buf = ENC.GetBytes("33333");
            s.Execute("GET", u, buf, 0, buf.Length, "text/plain");
            resp.Event.WaitOne();
            Assert.AreEqual("12345678901234567890", resp.Last);
        }
        void ISocketEventListener.OnConnect(BaseSocket sock)
        {
            lock (m_queue)
            {
                if (!m_running &&
                    m_sockA != null && m_sockA.Connected &&
                    m_sockB != null && m_sockB.Connected)
                {
                    m_running = true;
                    m_lastSock = m_sockB;

                    m_thread = new Thread(ProcessThread);
                    m_thread.IsBackground = true;
                    m_thread.Name = "XEP 124 processing thread";
                    m_thread.Start();

                    m_listener.OnConnect(this);
                }
            }            
        }
        /// <summary>
        /// Start polling
        /// </summary>
        /// <param name="addr">Ignored in this case.  Set URL.</param>
        public override void Connect(Address addr)
        {
            Debug.Assert(m_uri != null);

            m_rid = -1L;
            m_lastSock = null;
            m_running = false;

            // Create new ones each time, in case the URL has changed or something.
            m_sockA = new HttpSocket(this);
            m_sockB = new HttpSocket(this);

            m_sockA.Name = "A";
            m_sockB.Name = "B";

            m_sockA.ProxyURI = m_sockB.ProxyURI = m_proxyURI;
            m_sockA.ProxyCredentials = m_sockB.ProxyCredentials = m_proxyCredentials;

            m_sockA.Connect(m_uri);
            m_sockB.Connect(m_uri);
        }
        /// <summary>
        /// Stop polling.
        /// </summary>
        public override void Close()
        {
            Body body = CreateOpenBodyTag();
            body.Type = BodyType.terminate;

            Enqueue(body);
            
            if (m_thread != null)
                m_thread.Join();

            lock (m_queue)
            {
                m_running = false;
                m_thread = null;
                m_sockA = m_sockB = m_lastSock = null;
            }
            m_listener.OnClose(this);
        }
        // Must hold lock first.
        private HttpSocket GetSocket()
        {
            // Debug.Assert(!BothPending);

            // Switch to the other socket than the last one, assuming the other socket isn't pending.
            // If the other socket is pending, use the last one.
            HttpSocket other = (m_lastSock == m_sockA) ? m_sockB : m_sockA;

            if (!other.IsPending)
                m_lastSock = other;

            Debug.WriteLine("Socket: " + m_lastSock.Name);
            return m_lastSock;
        }