/// <summary>
        /// Handles the callback after sending a request.
        /// </summary>
        /// <param name="sender">The sender of the notification.</param>
        /// <param name="args">The event args.</param>
        private void SendRequestCallback(object sender, SendPostEventArgs args)
        {
            if (args.ErrorText != null)
            {
                string errorText = "Error in talking to HealthVault: " + args.ErrorText;
                InvokeApplicationResponseCallback(args.HealthVaultRequest, null, null, errorText);
                return;
            }

            CHBaseResponse response = null;

            try
            {
                response = new CHBaseResponse(args.ResponseData);
            }
            catch (InvalidOperationException)
            {
                string errorText = "Response was not a valid HealthVault response: " + args.ResponseData;
                InvokeApplicationResponseCallback(args.HealthVaultRequest, null, null, errorText);
                return;
            }

            // The token that is returned from GetAuthenticatedSessionToken has a limited lifetime. When it expires,
            // we will get an error here. We detect that situation, get a new token, and then re-issue the call.
            if (response.StatusCode == AuthenticatedSessionTokenExpired)
            {
                RefreshSessionToken(args);
                return;
            }

            InvokeApplicationResponseCallback(args.HealthVaultRequest, args.ResponseData, response, response.ErrorMessage);
        }
        /// <summary>
        /// Invoke the calling application's callback.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultText">The raw response.</param>
        /// <param name="response">The response object.</param>
        /// <param name="errorText">The error text, or null if successful. </param>
        private void InvokeApplicationResponseCallback(
            CHBaseRequest request,
            string resultText,
            CHBaseResponse response,
            string errorText)
        {
            CHBaseResponseEventArgs eventArgs = new CHBaseResponseEventArgs(request, resultText, response);

            eventArgs.ErrorText = errorText;

            if (request.ResponseCallback != null)
            {
                request.ResponseCallback(this, eventArgs);
            }
        }
        /// <summary>
        /// Initializes a new instance of the HealthVaultResponseEventArgs class.
        /// </summary>
        /// <param name="request">The request that is being processed.</param>
        /// <param name="responseXml">The raw response xml from the request.</param>
        /// <param name="response">A deserialized version of the request.</param>
        public CHBaseResponseEventArgs(
            CHBaseRequest request,
            string responseXml,
            CHBaseResponse response)
        {
            Request     = request;
            ResponseXml = responseXml;
            Response    = response;

            if (responseXml != null)
            {
                XElement element = XElement.Parse(responseXml);

                XElement status = element.Element("status");

                ErrorCode = Int32.Parse(status.Element("code").Value);
            }
        }
        /// <summary>
        /// Initializes a new instance of the HealthVaultResponseEventArgs class.
        /// </summary>
        /// <param name="request">The request that is being processed.</param>
        /// <param name="responseXml">The raw response xml from the request.</param>
        /// <param name="response">A deserialized version of the request.</param>
        public CHBaseResponseEventArgs(
            CHBaseRequest request,
            string responseXml,
            CHBaseResponse response)
        {
            Request = request;
            ResponseXml = responseXml;
            Response = response;

            if (responseXml != null)
            {
                XElement element = XElement.Parse(responseXml);

                XElement status = element.Element("status");

                ErrorCode = Int32.Parse(status.Element("code").Value);
            }
        }
        /// <summary>
        /// Handles the callback after sending a request.
        /// </summary>
        /// <param name="sender">The sender of the notification.</param>
        /// <param name="args">The event args.</param>
        private void SendRequestCallback(object sender, SendPostEventArgs args)
        {
            if (args.ErrorText != null)
            {
                string errorText = "Error in talking to HealthVault: " + args.ErrorText;
                InvokeApplicationResponseCallback(args.HealthVaultRequest, null, null, errorText);
                return;
            }

            CHBaseResponse response = null;
            try
            {
                response = new CHBaseResponse(args.ResponseData);
            }
            catch (InvalidOperationException)
            {
                string errorText = "Response was not a valid HealthVault response: " + args.ResponseData;
                InvokeApplicationResponseCallback(args.HealthVaultRequest, null, null, errorText);
                return;
            }

            // The token that is returned from GetAuthenticatedSessionToken has a limited lifetime. When it expires,
            // we will get an error here. We detect that situation, get a new token, and then re-issue the call.
            if (response.StatusCode == AuthenticatedSessionTokenExpired)
            {
                RefreshSessionToken(args);
                return;
            }

            InvokeApplicationResponseCallback(args.HealthVaultRequest, args.ResponseData, response, response.ErrorMessage);
        }
        /// <summary>
        /// Invoke the calling application's callback.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultText">The raw response.</param>
        /// <param name="response">The response object.</param>
        /// <param name="errorText">The error text, or null if successful. </param>
        private void InvokeApplicationResponseCallback(
            CHBaseRequest request,
            string resultText,
            CHBaseResponse response,
            string errorText)
        {
            CHBaseResponseEventArgs eventArgs = new CHBaseResponseEventArgs(request, resultText, response);
            eventArgs.ErrorText = errorText;

            if (request.ResponseCallback != null)
            {
                request.ResponseCallback(this, eventArgs);
            }
        }