/// <summary>
        /// Connects the outbound socket.
        /// </summary>
        public override void Connect()
        {
            int port = (int)m_listener[Options.PORT];
            Debug.Assert(port > 0);

            m_sock = CreateSocket();


            string to = (string)m_listener[Options.TO];
            Debug.Assert(to != null);

            string host = (string)m_listener[Options.NETWORK_HOST];
            if ((host == null) || (host == ""))
                host = to;

            string url = (string)m_listener[Options.POLL_URL];
            if ((url == null) || (url == ""))
            {
#if !__MonoCS__
                url = Address.LookupTXT("_xmppconnect.", to, "_xmpp-client-xbosh");
                if (url == null)
#endif
                    throw new ArgumentNullException("URL not found in DNS, and not specified", "URL");
            }
            ((IHttpSocket)m_sock).URL = url;

            //Address addr = new Address(host, port);
            m_sock.Connect(null, (string)m_listener[Options.SERVER_ID]);
        }
            public bool OnRead(BaseSocket sock, byte[] buf, int offset, int length)
            {
                string str = ENC.GetString(buf, offset, length);
                Console.WriteLine("SR: " + str);
                if (str.Contains("11111"))
                {
                    sock.Write(ENC.GetBytes(@"HTTP/1.1 200 OK
Content-Length: 10
Content-Type: text/plain

1234567890"));
                }
                else if (str.Contains("22222"))
                {
                    sock.Write(ENC.GetBytes(@"HTTP/1.1 200 OK
Content-Length: 10
Content-Type: text/plain

12345"));
                    sock.Write(ENC.GetBytes("67890"));
                }
                else if (str.Contains("33333"))
                {
                    sock.Write(ENC.GetBytes(@"HTTP/1.1 200 OK
Content-Length: 20
Content-Type: text/plain

12345"));
                    // Turning off Nagle didn't fix this.  Mrmph.
                    Thread.Sleep(300);
                    sock.Write(ENC.GetBytes("67890"));
                    Thread.Sleep(300);
                    sock.Write(ENC.GetBytes("12345"));
                    Thread.Sleep(300);
                    sock.Write(ENC.GetBytes("67890"));
                }
                return true;
            }
        /// <summary>
        /// Listens for an inbound connection.
        /// </summary>
        public override void Accept()
        {
            if (m_accept == null)
            {
                m_accept = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false);
                ((AsyncSocket)m_accept).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as
                    System.Security.Cryptography.X509Certificates.X509Certificate2;

                Address addr = new Address((string)m_listener[Options.NETWORK_HOST],
                    (int)m_listener[Options.PORT]);

                m_accept.Accept(addr);
            }
            m_accept.RequestAccept();
        }
 /// <summary>
 /// An invalid peer certificate was sent during SSL/TLS neogtiation.
 /// </summary>
 /// <param name="sock">The socket that experienced the error</param>
 /// <param name="certificate">The bad certificate</param>
 /// <param name="chain">The chain of CAs for the cert</param>
 /// <param name="sslPolicyErrors">A bitfield for the erorrs in the certificate.</param>
 /// <returns>True if the cert should be accepted anyway.</returns>
 public virtual bool OnInvalidCertificate(BaseSocket sock,
     System.Security.Cryptography.X509Certificates.X509Certificate certificate,
     System.Security.Cryptography.X509Certificates.X509Chain chain,
     System.Net.Security.SslPolicyErrors sslPolicyErrors)
 {
     return false;
 }
 /// <summary>
 /// Bytes were read from the socket.
 /// </summary>
 /// <param name="sock">The socket that was read from.</param>
 /// <param name="buf">The bytes that were read.</param>
 /// <returns>true if RequestRead() should be called automatically again</returns>
 /// <param name="offset">Offset into the buffer to start at</param>
 /// <param name="length">Number of bytes to use out of the buffer</param>
 public virtual bool OnRead(BaseSocket sock, byte[] buf, int offset, int length)
 {
     return true;
 }
 /// <summary>
 /// Connection was closed.
 /// </summary>
 /// <param name="sock">Closed socket.  Already closed!</param>
 public virtual void OnClose(BaseSocket sock)
 {
 }
 /// <summary>
 /// A new incoming connection was accepted.
 /// </summary>
 /// <param name="newsocket">Socket for new connection.</param>
 /// <returns>true if RequestAccept() should be called automatically again</returns>
 public virtual bool OnAccept(BaseSocket newsocket)
 {
     return true;
 }
 public bool OnInvalidCertificate(BaseSocket sock, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
 {
     throw new NotImplementedException("The method or operation is not implemented.");
 }
 /// <summary>
 /// An invalid peer certificate was sent during SSL/TLS neogtiation.
 /// </summary>
 /// <param name="sock">The socket that experienced the error</param>
 /// <param name="certificate">The bad certificate</param>
 /// <param name="chain">The chain of CAs for the cert</param>
 /// <param name="sslPolicyErrors">A bitfield for the erorrs in the certificate.</param>
 /// <returns>True if the cert should be accepted anyway.</returns>
 public bool OnInvalidCertificate(BaseSocket sock,
     System.Security.Cryptography.X509Certificates.X509Certificate certificate,
     System.Security.Cryptography.X509Certificates.X509Chain chain,
     System.Net.Security.SslPolicyErrors sslPolicyErrors)
 {
     return m_listener.InvalidCertificate(sock, certificate, chain, sslPolicyErrors);
 }
        public void OnWrite(BaseSocket sock, byte[] buf, int offset, int length)
        {
            int tim = (int)m_listener[Options.KEEP_ALIVE];
            if (tim > 0)
                m_timer.Change(tim, tim);

            m_listener.BytesWritten(buf, offset, length);
        }
        public bool OnRead(BaseSocket sock, byte[] buf, int offset, int length)
        {
            int tim = (int)m_listener[Options.KEEP_ALIVE];
            if (tim > 0)
                m_timer.Change(tim, tim);

            m_listener.BytesRead(buf, offset, length);
            try
            {
                m_elements.Push(buf, offset, length);
            }
            catch (Exception e)
            {
                ((ISocketEventListener)this).OnError(sock, e);
                sock.Close();
                return false;
            }
            return true;
        }
 public void OnError(BaseSocket sock, Exception ex)
 {
     m_listener[Options.REMOTE_CERTIFICATE] = null;
     //m_elements = null;
     m_timer.Change(Timeout.Infinite, Timeout.Infinite);
     m_listener.Errored(ex);
 }
 public void OnClose(BaseSocket sock)
 {
     //System.Windows.Forms.Application.DoEvents();
     //System.Threading.Thread.Sleep(1000);
     m_listener[Options.REMOTE_CERTIFICATE] = null;
     //m_elements = null;
     m_timer.Change(Timeout.Infinite, Timeout.Infinite);
     m_listener.Closed();
 }
        public void OnConnect(BaseSocket sock)
        {
#if !NO_SSL
            if ((bool)m_listener[Options.SSL])
            {
                AsyncSocket s = sock as AsyncSocket;
                m_listener[Options.REMOTE_CERTIFICATE] = s.RemoteCertificate;
            }
#endif
            m_listener.Connected();
        }
        public bool OnAccept(BaseSocket newsocket)
        {
            m_sock = newsocket;
            InitializeStream();
            m_listener.Accepted();

            // Don't accept any more connections until this one closes
            // yes, it will look like we're still listening until the old sock is free'd by GC.
            // don't want OnClose() to fire, though, so we can't close the previous sock.
            return false;
        }
            public void OnClose(BaseSocket sock)
            {

            }
 public void OnError(BaseSocket sock, Exception ex)
 {
     throw ex;
 }
 public ISocketEventListener GetListener(BaseSocket newSock)
 {
     throw new NotImplementedException("The method or operation is not implemented.");
 }
 public void OnWrite(BaseSocket sock, byte[] buf, int offset, int length)
 {
     Console.WriteLine("SW: " + ENC.GetString(buf, offset, length));
 }
 public bool OnAccept(BaseSocket newsocket)
 {
     throw new NotImplementedException("The method or operation is not implemented.");
 }
 /// <summary>
 /// We accepted a socket, and need to get a listener.
 /// If the return value is null, then the socket will be closed,
 /// and RequestAccept will ALWAYS be called.
 /// </summary>
 /// <param name="newSock">The new socket.</param>
 /// <returns>The listener for the *new* socket, as compared to
 /// the listener for the *listen* socket</returns>
 public virtual ISocketEventListener GetListener(BaseSocket newSock)
 {
     return this;
 }
 public void OnConnect(BaseSocket sock)
 {
 }
 /// <summary>
 /// Outbound connection was connected.
 /// </summary>
 /// <param name="sock">Connected socket.</param>
 public virtual void OnConnect(BaseSocket sock)
 {
 }
 public bool OnRead(BaseSocket sock, byte[] buf, int offset, int length)
 {
     Last = ENC.GetString(buf, offset, length);
     Console.WriteLine("RR: " + Last);
     Event.Set();
     return true;
 }
 /// <summary>
 /// An error happened in processing.  The socket is no longer open.
 /// </summary>
 /// <param name="sock">Socket in error</param>
 /// <param name="ec">Exception that caused the error</param>
 public virtual void OnError(BaseSocket sock, System.Exception ec)
 {
 }
 public void OnInit(BaseSocket newSock)
 {
 }
 /// <summary>
 /// Bytes were written to the socket.
 /// </summary>
 /// <param name="sock">The socket that was written to.</param>
 /// <param name="buf">The bytes that were written.</param>
 /// <param name="offset">Offset into the buffer to start at</param>
 /// <param name="length">Number of bytes to use out of the buffer</param>
 public virtual void OnWrite(BaseSocket sock, byte[] buf, int offset, int length)
 {
 }
 public bool OnAccept(BaseSocket newsocket)
 {
     AsyncSocket s = (AsyncSocket)newsocket;
     newsocket.RequestRead();
     return true;
 }
 /// <summary>
 /// An accept socket is about to be bound, or a connect socket is about to connect,
 /// or an incoming socket just came in.  Use this as an opportunity to
 /// </summary>
 /// <param name="newSock">The new socket that is about to be connected.</param>
 public virtual void OnInit(BaseSocket newSock)
 {
 }
 public void OnConnect(BaseSocket sock)
 {
     throw new NotImplementedException("The method or operation is not implemented.");
 }