public void EndCurrentWrite()
        {
            if (writeTimer != null)
            {
                writeTimer.Dispose();
                writeTimer = null;
            }

            currentWrite = null;
        }
        /// <summary>
        /// If possible, this method dequeues a write from the write queue and starts it.
        /// This is only possible if all of the following are true:
        ///  1) any previous write has completed
        ///  2) there's a write in the queue
        ///  3) and the socket is connected.
        /// 
        /// This method is thread safe.
        /// </summary>
        private void MaybeDequeueWrite(object ignore)
        {
            lock (lockObj)
            {
                if ((flags & kClosed) > 0)
                {
                    writeQueue.Clear();
                    return;
                }

                if ((currentWrite == null) && (stream != null))
                {
                    if ((flags & kPauseWrites) > 0)
                    {
                        // Don't do any reads yet.
                        // We're waiting for TLS negotiation to start and/or finish.
                    }
                    else if (writeQueue.Count > 0)
                    {
                        // Get the next object in the read queue
                        Object nextWrite = writeQueue.Dequeue();

                        if (nextWrite is AsyncSpecialPacket)
                        {
                            // Next write packet is a special instruction packet.
                            // Right now this can only mean a StartTLS instruction.
                            AsyncSpecialPacket specialWrite = (AsyncSpecialPacket)nextWrite;

                            // Update flags - this flag will be unset when TLS finishes
                            flags |= kPauseWrites;

                            // And attempt to start TLS
                            // This method won't do anything unless both kPauseReads and kPauseWrites are set.
                            MaybeStartTLS();
                        }
                        else
                        {
                            // Get the current write AsyncWritePacket
                            currentWrite = (AsyncWritePacket)nextWrite;

                            // Start time-out timer
                            if (currentWrite.timeout >= 0)
                            {
                                writeTimer = new System.Threading.Timer(new TimerCallback(stream_DidNotWrite),
                                                                        currentWrite,
                                                                        currentWrite.timeout,
                                                                        Timeout.Infinite);
                            }

                            try
                            {
                                DoSendBytes();
                            }
                            catch (Exception e)
                            {
                                CloseWithException(e);
                            }
                        }
                    }
                    else if ((flags & kCloseAfterWrites) > 0)
                    {
                        if ((flags & kCloseAfterReads) > 0)
                        {
                            if ((readQueue.Count == 0) && (currentRead == null))
                            {
                                Close(null);
                            }
                        }
                        else
                        {
                            Close(null);
                        }
                    }
                }
            }
        }