private static CookieCollection parseResponse(string value)
        {
            var cookies = new CookieCollection ();

              Cookie cookie = null;
              var pairs = splitCookieHeaderValue (value);
              for (int i = 0; i < pairs.Length; i++) {
            var pair = pairs [i].Trim ();
            if (pair.Length == 0)
              continue;

            if (pair.StartsWith ("version", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
            }
            else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
              var buffer = new StringBuilder (pair.GetValueInternal ("="), 32);
              if (i < pairs.Length - 1)
            buffer.AppendFormat (", {0}", pairs [++i].Trim ());

              DateTime expires;
              if (!DateTime.TryParseExact (
            buffer.ToString (),
            new [] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
            CultureInfo.CreateSpecificCulture ("en-US"),
            DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal,
            out expires))
            expires = DateTime.Now;

              if (cookie != null && cookie.Expires == DateTime.MinValue)
            cookie.Expires = expires.ToLocalTime ();
            }
            else if (pair.StartsWith ("max-age", StringComparison.InvariantCultureIgnoreCase)) {
              var max = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
              var expires = DateTime.Now.AddSeconds ((double) max);
              if (cookie != null)
            cookie.Expires = expires;
            }
            else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Path = pair.GetValueInternal ("=");
            }
            else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Domain = pair.GetValueInternal ("=");
            }
            else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) {
              var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase)
                     ? "\"\""
                     : pair.GetValueInternal ("=");

              if (cookie != null)
            cookie.Port = port;
            }
            else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Comment = pair.GetValueInternal ("=").UrlDecode ();
            }
            else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.CommentUri = pair.GetValueInternal ("=").Trim ('"').ToUri ();
            }
            else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Discard = true;
            }
            else if (pair.StartsWith ("secure", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Secure = true;
            }
            else if (pair.StartsWith ("httponly", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.HttpOnly = true;
            }
            else {
              if (cookie != null)
            cookies.Add (cookie);

              string name;
              string val = String.Empty;

              var pos = pair.IndexOf ('=');
              if (pos == -1) {
            name = pair;
              }
              else if (pos == pair.Length - 1) {
            name = pair.Substring (0, pos).TrimEnd (' ');
              }
              else {
            name = pair.Substring (0, pos).TrimEnd (' ');
            val = pair.Substring (pos + 1).TrimStart (' ');
              }

              cookie = new Cookie (name, val);
            }
              }

              if (cookie != null)
            cookies.Add (cookie);

              return cookies;
        }
        private int searchCookie(Cookie cookie)
        {
            var name = cookie.Name;
              var path = cookie.Path;
              var domain = cookie.Domain;
              var version = cookie.Version;

              for (int i = _list.Count - 1; i >= 0; i--) {
            var c = _list [i];
            if (c.Name.Equals (name, StringComparison.InvariantCultureIgnoreCase) &&
            c.Path.Equals (path, StringComparison.InvariantCulture) &&
            c.Domain.Equals (domain, StringComparison.InvariantCultureIgnoreCase) &&
            c.Version == version)
              return i;
              }

              return -1;
        }
 private static int compareCookieWithinSorted(Cookie x, Cookie y)
 {
     var ret = 0;
       return (ret = x.Version - y.Version) != 0
      ? ret
      : (ret = x.Name.CompareTo (y.Name)) != 0
        ? ret
        : y.Path.Length - x.Path.Length;
 }
        private static CookieCollection parseRequest(string value)
        {
            var cookies = new CookieCollection ();

              Cookie cookie = null;
              var version = 0;
              var pairs = splitCookieHeaderValue (value);
              for (int i = 0; i < pairs.Length; i++) {
            var pair = pairs [i].Trim ();
            if (pair.Length == 0)
              continue;

            if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) {
              version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
            }
            else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Path = pair.GetValueInternal ("=");
            }
            else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Domain = pair.GetValueInternal ("=");
            }
            else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) {
              var port = pair.Equals ("$port", StringComparison.InvariantCultureIgnoreCase)
                     ? "\"\""
                     : pair.GetValueInternal ("=");

              if (cookie != null)
            cookie.Port = port;
            }
            else {
              if (cookie != null)
            cookies.Add (cookie);

              string name;
              string val = String.Empty;

              var pos = pair.IndexOf ('=');
              if (pos == -1) {
            name = pair;
              }
              else if (pos == pair.Length - 1) {
            name = pair.Substring (0, pos).TrimEnd (' ');
              }
              else {
            name = pair.Substring (0, pos).TrimEnd (' ');
            val = pair.Substring (pos + 1).TrimStart (' ');
              }

              cookie = new Cookie (name, val);
              if (version != 0)
            cookie.Version = version;
            }
              }

              if (cookie != null)
            cookies.Add (cookie);

              return cookies;
        }
 private static int compareCookieWithinSort(Cookie x, Cookie y)
 {
     return (x.Name.Length + x.Value.Length) - (y.Name.Length + y.Value.Length);
 }
        internal void SetOrRemove(Cookie cookie)
        {
            var pos = searchCookie (cookie);
              if (pos == -1) {
            if (!cookie.Expired)
              _list.Add (cookie);

            return;
              }

              if (!cookie.Expired) {
            _list [pos] = cookie;
            return;
              }

              _list.RemoveAt (pos);
        }
        /// <summary>
        /// Copies the elements of the collection to the specified array of <see cref="Cookie"/>,
        /// starting at the specified <paramref name="index"/> in the <paramref name="array"/>.
        /// </summary>
        /// <param name="array">
        /// An array of <see cref="Cookie"/> that represents the destination of the elements
        /// copied from the collection.
        /// </param>
        /// <param name="index">
        /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
        /// at which copying begins.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="array"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="index"/> is less than zero.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The number of elements in the collection is greater than the available space from
        /// <paramref name="index"/> to the end of the destination <paramref name="array"/>.
        /// </exception>
        public void CopyTo(Cookie [] array, int index)
        {
            if (array == null)
            throw new ArgumentNullException ("array");

              if (index < 0)
            throw new ArgumentOutOfRangeException ("index", "Less than zero.");

              if (array.Length - index < _list.Count)
            throw new ArgumentException (
              "The number of elements in this collection is greater than the available space of the destination array.");

              _list.CopyTo (array, index);
        }
        /// <summary>
        /// Adds the specified <paramref name="cookie"/> to the collection.
        /// </summary>
        /// <param name="cookie">
        /// A <see cref="Cookie"/> to add.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="cookie"/> is <see langword="null"/>.
        /// </exception>
        public void Add(Cookie cookie)
        {
            if (cookie == null)
            throw new ArgumentNullException ("cookie");

              var pos = searchCookie (cookie);
              if (pos == -1) {
            _list.Add (cookie);
            return;
              }

              _list [pos] = cookie;
        }
        internal void AddHeader(string header)
        {
            int colon = header.IndexOf(':');

            if (colon == -1 || colon == 0)
            {
                context.ErrorMessage = "Bad Request";
                context.ErrorStatus  = 400;
                return;
            }

            string name  = header.Substring(0, colon).Trim();
            string val   = header.Substring(colon + 1).Trim();
            string lower = name.ToLowerInvariant();

            headers.SetInternal(name, val);
            switch (lower)
            {
            case "accept-language":
                user_languages = val.Split(',');     // yes, only split with a ','
                break;

            case "accept":
                accept_types = val.Split(',');     // yes, only split with a ','
                break;

            case "content-length":
                try
                {
                    //TODO: max. content_length?
                    content_length = Int64.Parse(val.Trim());
                    if (content_length < 0)
                    {
                        context.ErrorMessage = "Invalid Content-Length.";
                    }
                    cl_set = true;
                }
                catch
                {
                    context.ErrorMessage = "Invalid Content-Length.";
                }

                break;

            case "content-type":
            {
                var contents = val.Split(';');
                foreach (var content in contents)
                {
                    var tmp = content.Trim();
                    if (tmp.StartsWith("charset"))
                    {
                        var charset = GetValue(tmp, "=");
                        if (charset != null && charset.Length > 0)
                        {
                            try
                            {
                                // Support upnp/dlna devices - CONTENT-TYPE: text/xml ; charset="utf-8"\r\n
                                charset = charset.Trim('"');
                                var index = charset.IndexOf('"');
                                if (index != -1)
                                {
                                    charset = charset.Substring(0, index);
                                }

                                content_encoding = Encoding.GetEncoding(charset);
                            }
                            catch
                            {
                                context.ErrorMessage = "Invalid Content-Type header: " + charset;
                            }
                        }

                        break;
                    }
                }
            }
            break;

            case "referer":
                try
                {
                    referrer = new Uri(val);
                }
                catch
                {
                    referrer = new Uri("http://someone.is.screwing.with.the.headers.com/");
                }
                break;

            case "cookie":
                if (cookies == null)
                {
                    cookies = new CookieCollection();
                }

                string[] cookieStrings = val.Split(new char[] { ',', ';' });
                Cookie   current       = null;
                int      version       = 0;
                foreach (string cookieString in cookieStrings)
                {
                    string str = cookieString.Trim();
                    if (str.Length == 0)
                    {
                        continue;
                    }
                    if (str.StartsWith("$Version"))
                    {
                        version = Int32.Parse(Unquote(str.Substring(str.IndexOf('=') + 1)));
                    }
                    else if (str.StartsWith("$Path"))
                    {
                        if (current != null)
                        {
                            current.Path = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else if (str.StartsWith("$Domain"))
                    {
                        if (current != null)
                        {
                            current.Domain = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else if (str.StartsWith("$Port"))
                    {
                        if (current != null)
                        {
                            current.Port = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else
                    {
                        if (current != null)
                        {
                            cookies.Add(current);
                        }
                        current = new Cookie();
                        int idx = str.IndexOf('=');
                        if (idx > 0)
                        {
                            current.Name  = str.Substring(0, idx).Trim();
                            current.Value = str.Substring(idx + 1).Trim();
                        }
                        else
                        {
                            current.Name  = str.Trim();
                            current.Value = String.Empty;
                        }
                        current.Version = version;
                    }
                }
                if (current != null)
                {
                    cookies.Add(current);
                }
                break;
            }
        }