private static UrlStats recordUrlStats <T>(Uri uri, double duration, Envelope <T> envelope)
        {
            var key = uri.AbsolutePath;

            UrlStats stat = null;

            if (__perUrlStats.TryGetValue(key, out stat))
            {
                stat.TotalRequestCount    += 1;
                stat.TotalRequestDuration += duration;
                stat.LastRequestDuration   = duration;

                stat.AverageRequestDuration = Math.Round(stat.TotalRequestDuration / (double)stat.TotalRequestCount, 3);

                if (duration < stat.MinimumRequestDuration)
                {
                    stat.MinimumRequestDuration = duration;
                }
                if (duration > stat.MaxiumRequestDuration)
                {
                    stat.MaxiumRequestDuration = duration;
                }
            }
            else
            {
                stat = new UrlStats();
                stat.TotalRequestCount    = 1;
                stat.TotalRequestDuration = duration;
                stat.LastRequestDuration  = duration;

                stat.AverageRequestDuration = duration;

                stat.MaxiumRequestDuration  = duration;
                stat.MinimumRequestDuration = duration;
            }

            if (envelope != null)
            {
                stat.LastRequestBody  = envelope.RawRequestBody;
                stat.LastResponseBody = envelope.RawResponseBody;
            }
            else
            {
                stat.LastRequestBody  = "-";
                stat.LastResponseBody = "-";
            }

            __perUrlStats[key] = stat;

            return(stat);
        }
        /// <summary>
        /// Retrieves the UrlStats for the last request on the given thread.  If threadid is unspecified or 0, defaults to the current managed thread id.
        /// </summary>
        /// <returns></returns>
        public static UrlStats UrlStatsForLastRequest(int threadId = 0)
        {
            if (threadId == 0)
            {
                threadId = Thread.CurrentThread.ManagedThreadId;
            }

            UrlStats rv = null;

            if (__lastRequested.TryGetValue(threadId, out rv))
            {
                return(rv);
            }
            return(null);
        }