public RestResponse ParseWebException(WebException exc, TimeOutState timeoutstate)
        {
            var restresponse = new RestResponse();

            // Check to see if this is an HTTP error or a transport error.
            // In cases where an HTTP error occurs ( status code >= 400 )
            // return the underlying HTTP response, otherwise assume a
            // transport exception (ex: connection timeout) and
            // rethrow the exception
            if (exc.Response is HttpWebResponse)
            {
                var errorresponse = exc.Response as HttpWebResponse;
                restresponse = ExtractResponse(errorresponse);
            }
            else
            {
                restresponse.ErrorException = exc;
                restresponse.ErrorMessage   = exc.Message;

                if (timeoutstate != null && exc.Status == WebExceptionStatus.RequestCanceled)
                {
                    restresponse.ResponseStatus = timeoutstate.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted;
                }
                else
                {
                    restresponse.ResponseStatus = ResponseStatus.Error;
                }
            }

            return(restresponse);
        }
        internal RestRequestAsyncHandle ExecuteAsync(RestRequest restrequest, Action <RestResponse, RestRequestAsyncHandle> callback)
        {
            var asyncHandle = new RestRequestAsyncHandle();

            Action <RestResponse> responseCallback = r => callback(r, asyncHandle);

            var webrequest = this.WebRequest.ConfigureRequest(this, restrequest);

            _timeoutState = new TimeOutState {
                Request = webrequest
            };

            var asyncResult = webrequest.BeginGetResponse(r => ProcessHttpWebResponseCallback(r, responseCallback), webrequest);

            if (Timeout != 0)
            {
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _timeoutState, this.Timeout, true);
            }

            asyncHandle.WebRequest = webrequest;
            return(asyncHandle);
        }