private static void _completeGetRequestStream <TResponse>(IAsyncResult ar)
        {
            AsyncExecutionState <TResponse> r = (AsyncExecutionState <TResponse>)ar.AsyncState;

            System.IO.Stream reqstr;
            try
            {
                reqstr = r.request.EndGetRequestStream(ar);
            }
            catch (WebException wex)
            {
                r.handleWebException(wex);
                r.completedWithError(wex);
                r.clear();
                r      = null;
                reqstr = null;
                return;
            }
            catch (Exception ex)
            {
                r.handleException(ex);
                r.completedWithError(ex);
                r.clear();
                r      = null;
                reqstr = null;
                return;
            }

            try
            {
                using (reqstr)
                    reqstr.Write(r.bodyBytes, 0, r.bodyBytes.Length);
            }
            catch (WebException wex)
            {
                r.handleWebException(wex);
                r.completedWithError(wex);
                r.clear();
                r      = null;
                reqstr = null;
                return;
            }
            catch (Exception ex)
            {
                r.handleException(ex);
                r.completedWithError(ex);
                r.clear();
                r      = null;
                reqstr = null;
                return;
            }

            // Start an async operation to get the response:
            r.request.BeginGetResponse(_completeGetResponse <TResponse>, r);
        }
        private static void _completeGetResponse <TResponse>(IAsyncResult ar)
        {
            AsyncExecutionState <TResponse> r = (AsyncExecutionState <TResponse>)ar.AsyncState;

            HttpWebResponse rsp;

            try
            {
                rsp = (HttpWebResponse)r.request.EndGetResponse(ar);
            }
            catch (WebException wex)
            {
                r.handleWebException(wex);
                r.completedWithError(wex);
                r.clear();
                r = null;
                return;
            }
            catch (Exception ex)
            {
                r.handleException(ex);
                r.completedWithError(ex);
                r.clear();
                r = null;
                return;
            }

            try
            {
                using (var rs = rsp.GetResponseStream())
                {
                    Debug.Assert(r.deserializeBody != null);
                    var result = (TResponse)r.deserializeBody(rs, typeof(TResponse));
                    r.handleResponse(result);
                }
            }
            catch (WebException wex)
            {
                r.handleWebException(wex);
                r.completedWithError(wex);
                r.clear();
            }
            catch (Exception ex)
            {
                r.handleException(ex);
                r.completedWithError(ex);
                r.clear();
            }
            finally
            {
                rsp.Close();
                r.clear();
            }
        }