private ArrayManagedSocket BuildSocket(int index)
		{
			ArrayManagedSocket socket = new ArrayManagedSocket(this.Settings, this, index);
			socket.Connect(this.destination, this.Settings.ConnectTimeout);
			Interlocked.Increment(ref socketCount);
			return socket;

		}
        private ArrayManagedSocket BuildSocket(int index)
        {
            ArrayManagedSocket socket = new ArrayManagedSocket(this.Settings, this, index);

            socket.Connect(this.destination, this.Settings.ConnectTimeout);
            Interlocked.Increment(ref socketCount);
            return(socket);
        }
        internal override ManagedSocket GetSocket()
        {
            ArrayManagedSocket socket = null;

            lock (padlock)
            {
                while (socket == null)
                {
                    for (int i = 0; i < sockets.Length; i++)
                    {
                        if (sockets[i] == null)
                        {
                            sockets[i] = BuildSocket(i);
                            Interlocked.Increment(ref activeSocketCount);
                            socket = sockets[i];
                            break;
                        }

                        if (sockets[i].Idle)
                        {
                            try
                            {
                                if (sockets[i].LastError == SocketError.Success)
                                {
                                    sockets[i].Idle = false;
                                    Interlocked.Increment(ref activeSocketCount);
                                    socket = sockets[i];
                                }
                                else
                                {
                                    ReleaseAndDisposeSocket(sockets[i]);
                                }
                            }
                            catch (ObjectDisposedException)
                            {
                                sockets[i] = BuildSocket(i);
                                Interlocked.Increment(ref activeSocketCount);
                                socket = sockets[i];
                            }

                            break;
                        }
                    }
                    if (socket == null)
                    {
                        if (log.IsWarnEnabled)
                        {
                            log.WarnFormat("All sockets in a socket pool for {0} are in use.", destination);
                        }
                        if (sockets.Length < ArraySocketPool.MaximumPoolSize)
                        {
                            GrowPool();
                        }
                    }
                }
            }
            return(socket);
        }
 private void GrowPool()
 {
     if (log.IsInfoEnabled)
     {
         log.InfoFormat("Growing socket pool for {0} to size {1}.", destination, sockets.Length * 2);
     }
     ArrayManagedSocket[] newSockets = new ArrayManagedSocket[sockets.Length * 2];
     lock (growlock)
     {
         growing = true;
         for (int i = 0; i < sockets.Length; i++)
         {
             newSockets[i] = sockets[i];
         }
         sockets = newSockets;
         growing = false;
     }
 }
		private void GrowPool()
		{
            if (log.IsInfoEnabled)
                log.InfoFormat("Growing socket pool for {0} to size {1}.", destination, sockets.Length * 2);
			ArrayManagedSocket[] newSockets = new ArrayManagedSocket[sockets.Length * 2];
			lock (growlock)
			{
				growing = true;
				for (int i = 0; i < sockets.Length; i++)
				{
					newSockets[i] = sockets[i];
				}
				sockets = newSockets;
				growing = false;
			}
		}