/// <summary>
        ///   Performs the authentication using the SASL Plain authentication mechanism.
        /// </summary>
        public override void Authenticate()
        {
            if (RequestToken())
            {
                // Send authentication mechanism
                var auth = new Auth();
                auth.Value = BuildMessage();

                auth.Mechanism = XmppCodes.SaslXGoogleTokenMechanism;

                Connection.Send(auth);

                waitEvent.WaitOne();

                if (!AuthenticationFailed)
                {
                    // Re-Initialize XMPP Stream
                    Connection.InitializeXmppStream();

                    // Wait until we receive the Stream features
                    Connection.WaitForStreamFeatures();
                }
            }
            else
            {
                AuthenticationFailed = true;
            }
        }
        /// <summary>
        ///   Performs the authentication using the SASL digest authentication mechanism.
        /// </summary>
        public override void Authenticate()
        {
            // Send authentication mechanism
            var auth = new Auth { Mechanism = XmppCodes.SaslDigestMD5Mechanism };

            Connection.Send(auth);

            successEvent.WaitOne();

            // Verify received Digest-Challenge

            // Check that the nonce setting is pressent
            if (!digestChallenge.ContainsKey("nonce"))
            {
                throw new XmppException("SASL Authrization failed. Incorrect challenge received from server");
            }

            // Check that the charset is correct
            if (digestChallenge.ContainsKey("charset")
                && digestChallenge["charset"] != "utf-8")
            {
                throw new XmppException("SASL Authrization failed. Incorrect challenge received from server");
            }

            // Check that the mechanims is correct
            if (!digestChallenge.ContainsKey("algorithm")
                || digestChallenge["algorithm"] != "md5-sess")
            {
                throw new XmppException("SASL Authrization failed. Incorrect challenge received from server");
            }

            // Send the Digest-Reponse
            var digestResponse = new Response();

            digestResponse.Value = BuildDigestRespose();

            Connection.Send(digestResponse);

            successEvent.WaitOne();

            if (digestChallenge.ContainsKey("rspauth"))
            {
                digestResponse = new Response();
                Connection.Send(digestResponse);

                successEvent.WaitOne();
            }

            if (!AuthenticationFailed)
            {
                // Re-Initialize XMPP Stream
                Connection.InitializeXmppStream();

                // Wait until we receive the Stream features
                Connection.WaitForStreamFeatures();
            }
        }