internal async Task Release(Task <TcpServerConnection> connectionCreateTask, bool closeServerConnection)
 {
     if (connectionCreateTask != null)
     {
         TcpServerConnection connection = null;
         try
         {
             connection = await connectionCreateTask;
         }
         catch { }
         finally
         {
             await Release(connection, closeServerConnection);
         }
     }
 }
        /// <summary>
        ///     Release connection back to cache.
        /// </summary>
        /// <param name="connection">The Tcp server connection to return.</param>
        /// <param name="close">Should we just close the connection instead of reusing?</param>
        internal async Task Release(TcpServerConnection connection, bool close = false)
        {
            if (connection == null)
            {
                return;
            }

            if (close || connection.IsWinAuthenticated || !Server.EnableConnectionPool || connection.IsClosed)
            {
                disposalBag.Add(connection);
                return;
            }

            connection.LastAccess = DateTime.Now;

            try
            {
                await @lock.WaitAsync();

                while (true)
                {
                    if (cache.TryGetValue(connection.CacheKey, out var existingConnections))
                    {
                        while (existingConnections.Count >= Server.MaxCachedConnections)
                        {
                            if (existingConnections.TryDequeue(out var staleConnection))
                            {
                                disposalBag.Add(staleConnection);
                            }
                        }

                        existingConnections.Enqueue(connection);
                        break;
                    }

                    if (cache.TryAdd(connection.CacheKey,
                                     new ConcurrentQueue <TcpServerConnection>(new[] { connection })))
                    {
                        break;
                    }
                }
            }
            finally
            {
                @lock.Release();
            }
        }