protected virtual Task <OrientdbResponse <Stream> > DoAsyncRequest(HttpWebRequest request, byte[] data         = null,
                                                                           IRequestConfiguration requestSpecificConfig = null)
        {
            var tcs = new TaskCompletionSource <OrientdbResponse <Stream> >();

            if (ConnectionSettings.MaximumAsyncConnections <= 0 ||
                _resourceLock == null)
            {
                return(CreateIterateTask(request, data, requestSpecificConfig, tcs));
            }

            int    timeout = GetRequestTimeout(requestSpecificConfig);
            string path    = request.RequestUri.ToString();
            string method  = request.Method;

            if (!_resourceLock.WaitOne(timeout))
            {
                string m = "Could not start the operation before the timeout of " + timeout +
                           "ms completed while waiting for the semaphore";
                OrientdbResponse <Stream> cs = OrientdbResponse <Stream> .CreateError(ConnectionSettings,
                                                                                      new TimeoutException(m), method,
                                                                                      path, data);

                tcs.SetResult(cs);
                return(tcs.Task);
            }
            try
            {
                return(CreateIterateTask(request, data, requestSpecificConfig, tcs));
            }
            finally
            {
                _resourceLock.Release();
            }
        }
        private OrientdbResponse <Stream> HandleWebException(byte[] data, WebException webException, string method,
                                                             string path)
        {
            OrientdbResponse <Stream> cs;
            var httpEx = webException.Response as HttpWebResponse;

            if (httpEx != null)
            {
                //StreamReader ms = new StreamReader(httpEx.GetResponseStream());
                //var response = ms.ReadToEnd();
                cs = WebToOrientdbResponse(data, httpEx.GetResponseStream(), httpEx, method, path);
                return(cs);
            }
            cs = OrientdbResponse <Stream> .CreateError(ConnectionSettings, webException, method, path, data);

            return(cs);
        }