/// <summary>
        /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
        /// </summary>
        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
        /// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>buffer</b> is null reference.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        /// <exception cref="NotSupportedException">Is raised when reading not supported.</exception>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentException("Invalid argument 'offset' value.");
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentException("Invalid argument 'count' value.");
            }
            if ((m_AccessMode & FileAccess.Read) == 0)
            {
                throw new NotSupportedException();
            }

            while (true)
            {
                // Read next quoted-printable line and decode it.
                if (m_DecodedOffset >= m_DecodedCount)
                {
                    m_DecodedOffset = 0;
                    m_DecodedCount = 0;
                    SmartStream.ReadLineAsyncOP readLineOP = new SmartStream.ReadLineAsyncOP(line_buf,
                                                                                             SizeExceededAction
                                                                                                 .
                                                                                                 ThrowException);
                    m_pStream.ReadLine(readLineOP, false);
                    // IO error reading line.
                    if (readLineOP.Error != null)
                    {
                        throw readLineOP.Error;
                    }
                        // We reached end of stream.
                    else if (readLineOP.BytesInBuffer == 0)
                    {
                        return 0;
                    }
                        // Decode quoted-printable line.
                    else
                    {
                        // Process bytes.
                        bool softLineBreak = false;
                        int lineLength = readLineOP.LineBytesInBuffer;
                        for (int i = 0; i < readLineOP.LineBytesInBuffer; i++)
                        {
                            byte b = readLineOP.Buffer[i];
                            // We have soft line-break.
                            if (b == '=' && i == (lineLength - 1))
                            {
                                softLineBreak = true;
                            }
                                // We should have =XX char.
                            else if (b == '=')
                            {
                                byte b1 = readLineOP.Buffer[++i];
                                byte b2 = readLineOP.Buffer[++i];

                                string b1b2 = ((char)b1).ToString() + (char)b2;
                                byte b1b2_num;
                                if (byte.TryParse(b1b2, NumberStyles.HexNumber, null, out b1b2_num))
                                {
                                    m_pDecodedBuffer[m_DecodedCount++] = b1b2_num;
                                }
                                else
                                {
                                    m_pDecodedBuffer[m_DecodedCount++] = b;
                                    m_pDecodedBuffer[m_DecodedCount++] = b1;
                                    m_pDecodedBuffer[m_DecodedCount++] = b2;
                                }
                            }
                            // Normal char.
                            else
                            {
                                m_pDecodedBuffer[m_DecodedCount++] = b;
                            }
                        }

                        if (!softLineBreak)
                        {
                            m_pDecodedBuffer[m_DecodedCount++] = (byte) '\r';
                            m_pDecodedBuffer[m_DecodedCount++] = (byte) '\n';
                        }
                    }
                }

                // We some decoded data, return it.
                if (m_DecodedOffset < m_DecodedCount)
                {
                    int countToCopy = Math.Min(count, m_DecodedCount - m_DecodedOffset);
                    Array.Copy(m_pDecodedBuffer, m_DecodedOffset, buffer, offset, countToCopy);
                    m_DecodedOffset += countToCopy;

                    return countToCopy;
                }
            }
        }
        /// <summary>
        /// Starts recieveing command.
        /// </summary>
        private void BeginRecieveCmd()
        {
            SmartStream.ReadLineAsyncOP args = new SmartStream.ReadLineAsyncOP(new byte[Workaround.Definitions.MaxStreamLineLength],
                                                                               SizeExceededAction.
                                                                                   JunkAndThrowException);

            args.Completed += new EventHandler<EventArgs<SmartStream.ReadLineAsyncOP>>(ReciveCmdComleted);
            TcpStream.ReadLine(args, true);
        }
        internal void ReadAsync(EventHandler<EventArgs<SmartStream.ReadLineAsyncOP>> completed)
        {
            SmartStream.ReadLineAsyncOP args = new SmartStream.ReadLineAsyncOP(new byte[Workaround.Definitions.MaxStreamLineLength],
                                                                               SizeExceededAction.
                                                                                   JunkAndThrowException);

            args.Completed += completed;
            TcpStream.ReadLine(args, true);
        }
 private string ReadLine()
 {
     SmartStream.ReadLineAsyncOP args = new SmartStream.ReadLineAsyncOP(new byte[Workaround.Definitions.MaxStreamLineLength],
                                                                        SizeExceededAction.
                                                                            JunkAndThrowException);
     if (TcpStream.ReadLine(args, false))
     {
         return args.LineUtf8;
     }
     return string.Empty;
 }