////        internal static T GetResponseData2<T>(IRequestInfo requestInfo)
////        {
////            if (requestInfo == null)
////            {
////                throw new ArgumentNullException("requestInfo");
////            }

////            var webRequest = (HttpWebRequest)WebRequest.Create(requestInfo.Url);

////#if !SILVERLIGHT
////            webRequest.Referer = requestInfo.Referrer;
////#endif

////            if (!string.IsNullOrEmpty(requestInfo.PostContent))
////            {
////#if PocketPC || SILVERLIGHT
////                webRequest.Method = "POST";
////#else
////                webRequest.Method = WebRequestMethods.Http.Post;
////#endif

////                var postBytes = Encoding.GetBytes(requestInfo.PostContent);

////#if SILVERLIGHT
////                webRequest.Headers[HttpRequestHeader.ContentLength] = postBytes.Length.ToString();
////#else
////                webRequest.ContentLength = postBytes.Length;
////#endif

////                var requestStream = GetRequestStream(webRequest, requestInfo.OpenTimeout);

////                WriteRequestStream(requestStream, postBytes, requestInfo.SendTimeout);
////            }

////            string resultString = GetResultString(webRequest, requestInfo);

////            return Deserialize<T>(resultString);
////        }

////        private static Stream GetRequestStream(WebRequest webRequest, TimeSpan timeout)
////        {
////            return Invoke<Stream>(webRequest.BeginGetRequestStream, webRequest.EndGetRequestStream, timeout);
////        }

////        private static void WriteRequestStream(Stream stream, byte[] postBytes, TimeSpan timeout)
////        {
////            using (stream)
////            {
////                stream.WriteTimeout = (int)timeout.TotalMilliseconds;
////                stream.Write(postBytes, 0, postBytes.Length);
////            }
////        }

////        private static string GetResultString(WebRequest webRequest, IRequestInfo requestInfo)
////        {
////            try
////            {
////                // HACK: Not sure it should be OpenTimeout, CloseTimeout or ReceiveTimeout.
////                using (var webResponse = GetResponse(webRequest, requestInfo.OpenTimeout))
////                {
////                    using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding))
////                    {
////                        reader.BaseStream.ReadTimeout = (int)requestInfo.ReceiveTimeout.TotalMilliseconds;
////                        return reader.ReadToEnd();
////                    }
////                }
////            }
////            catch (WebException ex)
////            {
////                throw new GoogleAPIException("Failed to get response.", ex);
////            }
////            catch (IOException ex)
////            {
////                throw new GoogleAPIException("Cannot read the response stream.", ex);
////            }
////        }

////        private static WebResponse GetResponse(WebRequest webRequest, TimeSpan timeout)
////        {
////            return Invoke<WebResponse>(webRequest.BeginGetResponse, webRequest.EndGetResponse, timeout);
////        }

////        private static T Invoke<T>(Func<AsyncCallback, object, IAsyncResult> beginInvoke, Func<IAsyncResult, T> endInvoke, TimeSpan timeout)
////        {
////            Thread threadToKill = Thread.CurrentThread;

////            var asyncResult = beginInvoke(null, null);
////#if PocketPC
////            if (!asyncResult.AsyncWaitHandle.WaitOne((int)timeout.TotalMilliseconds, false))
////#else
////            if (!asyncResult.AsyncWaitHandle.WaitOne(timeout))
////#endif
////            {
////                threadToKill.Abort();
////                throw new TimeoutException();
////            }

////            return endInvoke(asyncResult);
////        }

        public static IAsyncResult BeginGetResponseData(IRequestInfo requestInfo, AsyncCallback callback, object state)
        {
            if (requestInfo == null)
            {
                throw new ArgumentNullException("requestInfo");
            }

            var webRequest = (HttpWebRequest)WebRequest.Create(requestInfo.Url);

#if !SILVERLIGHT
            webRequest.Referer = requestInfo.Referrer;
#endif

            var getResponseAsyncResult = new GetResponseAsyncResult(webRequest, state);

            var innerAsyncResult = webRequest.BeginGetResponse(
                asyncResult =>
            {
                getResponseAsyncResult.InnerAsyncResult = asyncResult;

                if (callback != null)
                {
                    callback(getResponseAsyncResult);
                }
            },
                null);

            getResponseAsyncResult.InnerAsyncResult = innerAsyncResult;

            return(getResponseAsyncResult);
        }
            public static object End(IAsyncResult result, out object[] outputs)
            {
                GetResponseAsyncResult thisPtr = AsyncResult.End <GetResponseAsyncResult>(result);

                return(thisPtr.context.GetResponse(out outputs));
            }
 internal object EndGetResponse(IAsyncResult result, out object[] outputs)
 {
     return(GetResponseAsyncResult.End(result, out outputs));
 }
 internal IAsyncResult BeginGetResponse(TimeSpan timeout, AsyncCallback callback, object state)
 {
     Fx.Assert(this.responseWaitHandle != null, "this.responseWaitHandle must not be null!");
     return(GetResponseAsyncResult.Create(this, timeout, callback, state));
 }
            static void HandleEndWait(object state, TimeoutException e)
            {
                GetResponseAsyncResult thisPtr = (GetResponseAsyncResult)state;

                thisPtr.Complete(false, e);
            }
 internal IAsyncResult BeginGetResponse(TimeSpan timeout, AsyncCallback callback, object state)
 {
     return(GetResponseAsyncResult.Create(this, timeout, callback, state));
 }