void ISocketEventListener.OnError(BaseSocket sock, Exception ex)
 {
     m_listener[Options.REMOTE_CERTIFICATE] = null;
     m_elements = null;
     m_listener.Errored(ex);
 }
 void ISocketEventListener.OnClose(BaseSocket sock)
 {
     m_listener[Options.REMOTE_CERTIFICATE] = null;
     m_elements = null;
     m_listener.Closed();
 }
            /// <summary>
            /// Some XML parsing error occurred.  Wrap it, and generate a little more context, so that we can try
            /// to figure out where the actual error happened.
            /// </summary>
            /// <param name="innerException"></param>
            /// <param name="stream"></param>
            /// <param name="buf"></param>
            /// <param name="offset"></param>
            /// <param name="length"></param>
            public XMLParseException(Exception innerException, AsynchElementStream stream, byte[] buf, int offset, int length)
                : base("Parsing exception", innerException)
            {
                XmlElement e = stream.m_elem;
                XmlElement last = null;
                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                while (e != null)
                {
                    last = e;
                    e = e.ParentNode as XmlElement;
                }

                if (last != null)
                {
                    sb.Append("Outer element: ");
                    sb.Append(last.OuterXml);
                    sb.Append("\n");
                }
                else
                {
                    sb.Append("Root stanza\n");
                }

                sb.Append("New text (note: it's normal to see what looks like extra close tags here): ");
                try
                {
                    sb.Append(AsynchElementStream.utf.GetString(buf, offset, length));
                }
                catch (Exception)
                {
                    sb.Append("Error in UTF8 decode: ");
                    sb.Append(Element.HexString(buf, offset, length));
                }
                m_context = sb.ToString();
            }
        /// <summary>
        /// Sets up the element stream.  This is the place to add factories.
        /// </summary>
        public override void InitializeStream()
        {
            bool first = (m_elements == null);
            m_elements = new AsynchElementStream();
            m_elements.OnDocumentStart += new ProtocolHandler(m_elements_OnDocumentStart);
            m_elements.OnDocumentEnd += new bedrock.ObjectHandler(m_elements_OnDocumentEnd);
            m_elements.OnElement += new ProtocolHandler(m_elements_OnElement);
            m_elements.OnError += new bedrock.ExceptionHandler(m_elements_OnError);

            m_listener.StreamInit(m_elements);

            Debug.Assert(this.Connected);
            if (first)
                m_sock.RequestRead();
        }
        /// <summary>
        /// Connects to the XMPP server.
        /// </summary>
        public override void Connect()
        {
            m_elements = null;
            int port = (int)m_listener[Options.PORT];
            Debug.Assert(port > 0);
            //m_sslOn = m_ssl;

            ProxySocket proxy = null;
            ProxyType pt = (ProxyType)m_listener[Options.PROXY_TYPE];
            switch (pt)
            {
            case ProxyType.Socks4:
                proxy = new Socks4Proxy(this);
                break;

            case ProxyType.Socks5:
                proxy = new Socks5Proxy(this);
                break;

            case ProxyType.HTTP:
                proxy = new ShttpProxy(this);
                break;

                /*
            case ProxyType.HTTP_Polling:
                XEP25Socket j25s = new XEP25Socket(this);
                if (m_ProxyHost != null)
                {
                    System.Net.WebProxy wp = new System.Net.WebProxy();
                    wp.Address = new Uri("http://" + m_ProxyHost + ":" + m_ProxyPort);
                    if (m_ProxyUsername != null)
                    {
                        wp.Credentials = new System.Net.NetworkCredential(m_ProxyUsername, m_ProxyPassword);
                    }
                    j25s.Proxy = wp;
                }
                j25s.URL = m_server;
                m_sock = j25s;
                break;
                */
            case ProxyType.None:
                m_sock = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false);

                ((AsyncSocket)m_sock).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as
                    System.Security.Cryptography.X509Certificates.X509Certificate2;

                ((AsyncSocket)m_sock).CertificateGui = (bool)m_listener[Options.CERTIFICATE_GUI];
                break;

            default:
                throw new ArgumentException("no handler for proxy type: " + pt, "ProxyType");
            }

            if (proxy != null)
            {
                proxy.Socket = new AsyncSocket(null, proxy, (bool)m_listener[Options.SSL], false);
                ((AsyncSocket)proxy.Socket).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as
                    System.Security.Cryptography.X509Certificates.X509Certificate2;

                proxy.Host = m_listener[Options.PROXY_HOST] as string;
                proxy.Port = (int)m_listener[Options.PROXY_PORT];
                proxy.Username = m_listener[Options.PROXY_USER] as string;
                proxy.Password = m_listener[Options.PROXY_PW] as string;
                m_sock = proxy;
            }

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

            string host = (string)m_listener[Options.NETWORK_HOST];
            if (string.IsNullOrEmpty(host))
            {
                try
                {
                    Address.LookupSRV((string)m_listener[Options.SRV_PREFIX], to, ref host, ref port);
                }
                catch
                {
                    Debug.WriteLine("WARNING: netlib.Dns.dll missing");
                    host = to;
                }
            }

            Address addr = new Address(host, port);
            m_sock.Connect(addr, (string)m_listener[Options.SERVER_ID]);
        }