private byte[] GetOpenOrClosePacket(byte[] outgoingData)
        {
            if (outgoingData.Length > 4 && outgoingData[3] == ' ')
            {
                // Be fast...
                switch ((char)outgoingData[0])
                {
                case 'V':     // VER
                    openState[0] = (outgoingData[1] == 'E' && outgoingData[2] == 'R');
                    openCommand  = NetworkMessage.AppendArray(openCommand, outgoingData);
                    break;

                case 'C':     // CVR
                    openState[1] = (outgoingData[1] == 'V' && outgoingData[2] == 'R');
                    openCommand  = NetworkMessage.AppendArray(openCommand, outgoingData);
                    break;

                case 'U':     // USR
                    openState[2] = (outgoingData[1] == 'S' && outgoingData[2] == 'R');
                    openCommand  = NetworkMessage.AppendArray(openCommand, outgoingData);
                    break;

                case 'O':     // OUT
                    openState[3] = (outgoingData[1] == 'U' && outgoingData[2] == 'T');
                    // Don't buffer OUT packet
                    break;
                }
            }

            if (openState.Get(0) && openState.Get(1) && openState.Get(2))
            {
                opened = true;
                action = HttpPollAction.Open;
                return(openCommand);
            }
            else if (openState.Get(3))
            {
                // Special case, not connected, but the packet is OUT
                isWebRequestInProcess = false;
                // This fires Disconnected event (send only 1 OUT packet)
                if (isWebResponseHeadersProcessed)
                {
                    return(outgoingData);
                }
                else
                {
                    isWebResponseHeadersProcessed = false;
                    OnDisconnected();
                    return(null);
                }
            }

            return(null); // Connection has not been established yet
        }
        private void pollTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            byte[]        buffer     = new byte[0];
            List <object> userStates = new List <object>();

            lock (SyncObject)
            {
                while (sendingQueue.Count > 0)
                {
                    QueueState queueState = sendingQueue.Dequeue();

                    if (queueState.UserState is Array)
                    {
                        foreach (object us in queueState.UserState as object[])
                        {
                            if (!userStates.Contains(us))
                            {
                                userStates.Add(us);
                            }
                        }
                    }
                    else
                    {
                        if (!userStates.Contains(queueState.UserState))
                        {
                            userStates.Add(queueState.UserState);
                        }
                    }

                    buffer = NetworkMessage.AppendArray(buffer, queueState.Data);

                    if (buffer.Length > MaxAllowedPacket)
                    {
                        break;
                    }
                }

                action = (buffer.Length > 0) ? HttpPollAction.None : HttpPollAction.Poll;
            }

            Send(buffer, userStates.ToArray());
        }