protected HttpClientContext CreateContext(bool isSecured, IPEndPoint endPoint, Stream stream, Socket sock)
{
HttpClientContext context;
lock (_contextQueue)
{
if (_contextQueue.Count > 0)
{
context = _contextQueue.Dequeue();
if (!context.Available)
{
context = CreateNewContext(isSecured, endPoint, stream, sock);
context.Disconnected += OnFreeContext;
context.RequestReceived += OnRequestReceived;
context.EndWhenDone = true;
}
}
else
{
context = CreateNewContext(isSecured, endPoint, stream, sock);
context.Disconnected += OnFreeContext;
context.RequestReceived += OnRequestReceived;
}
}
context.Stream = stream;
context.IsSecured = isSecured;
context.RemotePort = endPoint.Port.ToString();
context.RemoteAddress = endPoint.Address.ToString();
context.Start();
return context;
}