/// <summary>
        /// This constructor uses a Negotiate Listener if one isn't passed.
        /// 
        /// Initializes a new instance of the <see cref="WebDavServer" /> class.
        /// </summary>
        /// <param name="store">The 
        /// <see cref="IWebDavStore" /> store object that will provide
        /// collections and documents for this 
        /// <see cref="WebDavServer" />.</param>
        /// <param name="listener">The 
        /// <see cref="IHttpListener" /> object that will handle the web server portion of
        /// the WebDAV server; or 
        /// <c>null</c> to use a fresh one.</param>
        /// <param name="methodHandlers">A collection of HTTP method handlers to use by this 
        /// <see cref="WebDavServer" />;
        /// or 
        /// <c>null</c> to use the built-in method handlers.</param>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="listener" /> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="System.ArgumentException"><para>
        ///   <paramref name="methodHandlers" /> is empty.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="methodHandlers" /> contains a <c>null</c>-reference.</para></exception>
        public WebDavServer(IWebDavStore store, IHttpListener listener = null, IEnumerable<IWebDavMethodHandler> methodHandlers = null)
        {
            if (store == null)
                throw new ArgumentNullException("store");
            if (listener == null)
            {
                listener = new HttpListenerNegotiateAdapter();
                _ownsListener = true;
            }
            methodHandlers = methodHandlers ?? WebDavMethodHandlers.BuiltIn;

            IWebDavMethodHandler[] webDavMethodHandlers = methodHandlers as IWebDavMethodHandler[] ?? methodHandlers.ToArray();

            if (!webDavMethodHandlers.Any())
                throw new ArgumentException("The methodHandlers collection is empty", "methodHandlers");
            if (webDavMethodHandlers.Any(methodHandler => methodHandler == null))
                throw new ArgumentException("The methodHandlers collection contains a null-reference", "methodHandlers");

            _listener = listener;
            _store = store;
            var handlersWithNames =
                from methodHandler in webDavMethodHandlers
                from name in methodHandler.Names
                select new
                {
                    name,
                    methodHandler
                };
            _methodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler);
        }
        /// <summary>
        /// This constructor uses a Negotiate Listener if one isn't passed.
        /// 
        /// Initializes a new instance of the <see cref="WebDavServer" /> class.
        /// </summary>
        /// <param name="store">The 
        /// <see cref="IWebDavStore" /> store object that will provide
        /// collections and documents for this 
        /// <see cref="WebDavServer" />.</param>
        /// <param name="listener">The 
        /// <see cref="IHttpListener" /> object that will handle the web server portion of
        /// the WebDAV server; or 
        /// <c>null</c> to use a fresh one.</param>
        /// <param name="methodHandlers">A collection of HTTP method handlers to use by this 
        /// <see cref="WebDavServer" />;
        /// or 
        /// <c>null</c> to use the built-in method handlers.</param>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="listener" /> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="System.ArgumentException"><para>
        ///   <paramref name="methodHandlers" /> is empty.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="methodHandlers" /> contains a <c>null</c>-reference.</para></exception>
        public WebDavServer(IWebDavStore store, IHttpListener listener = null, IEnumerable<IWebDavMethodHandler> methodHandlers = null)
        {
            if (store == null)
                throw new ArgumentNullException("store");

            if (listener == null)
            {
                listener = new HttpListenerNegotiateAdapter();
                _ownsListener = true;
            }

            _listener = listener;
            _store = store;

            _webDavRequestProcessor = new WebDavRequestProcessor(store, methodHandlers);

        }
        /// <summary>
        ///     This constructor uses a Negotiate Listener if one isn't passed.
        ///     Initializes a new instance of the <see cref="WebDavServer" /> class.
        /// </summary>
        /// <param name="store">
        ///     The
        ///     <see cref="IWebDavStore" /> store object that will provide
        ///     collections and documents for this
        ///     <see cref="WebDavServer" />.
        /// </param>
        /// <param name="listener">
        ///     The
        ///     <see cref="IHttpListener" /> object that will handle the web server portion of
        ///     the WebDAV server; or
        ///     <c>null</c> to use a fresh one.
        /// </param>
        /// <param name="methodHandlers">
        ///     A collection of HTTP method handlers to use by this
        ///     <see cref="WebDavServer" />;
        ///     or
        ///     <c>null</c> to use the built-in method handlers.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        ///     <para>
        ///         <paramref name="listener" /> is <c>null</c>.
        ///     </para>
        ///     <para>- or -</para>
        ///     <para>
        ///         <paramref name="store" /> is <c>null</c>.
        ///     </para>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        ///     <para>
        ///         <paramref name="methodHandlers" /> is empty.
        ///     </para>
        ///     <para>- or -</para>
        ///     <para>
        ///         <paramref name="methodHandlers" /> contains a <c>null</c>-reference.
        ///     </para>
        /// </exception>
        public WebDavServer(ref IWebDavStore store, IHttpListener listener = null, IEnumerable<IWebDavMethodHandler> methodHandlers = null)
        {
            ServicePointManager.DefaultConnectionLimit = DefaultConnectionLimit;
            ServicePointManager.Expect100Continue = Expect100Continue;
            ServicePointManager.MaxServicePoints = MaxServicePoints;
            ServicePointManager.MaxServicePointIdleTime = int.MaxValue;
            if (store == null)
                throw new ArgumentNullException(nameof(store));
            if (listener == null)
            {
                listener = new HttpListenerNegotiateAdapter();
                _ownsListener = true;
            }
            methodHandlers = methodHandlers ?? WebDavMethodHandlers.BuiltIn;

            IWebDavMethodHandler[] webDavMethodHandlers = methodHandlers as IWebDavMethodHandler[] ?? methodHandlers.ToArray();

            if (!webDavMethodHandlers.Any())
                throw new ArgumentException("The methodHandlers collection is empty", nameof(methodHandlers));
            if (webDavMethodHandlers.Any(methodHandler => methodHandler == null))
                throw new ArgumentException("The methodHandlers collection contains a null-reference", nameof(methodHandlers));

            _listener = listener;
            _store = store;
            //_store.FClearCaches = DoClearCaches;
            var handlersWithNames =
                from methodHandler in webDavMethodHandlers
                from name in methodHandler.Names
                select new
                {
                    name,
                    methodHandler
                };
            _methodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler);
        }