'Handle a bunch of incoming bytes. This is a generator. It will yield
all characters not of interest for Telnet/RFC 2217.
The idea is that the reader thread pushes data from the socket through
this filter:
for byte in filter(socket.recv(1024)):
# do things like CR/LF conversion/whatever
# and write data to the serial port
serial.write(byte)
(socket error handling code left as exercise for the reader)'

def filter(self, data):

    for byte in iterbytes(data):
        if (self.mode == M_NORMAL):
            if (byte == IAC):
                self.mode = M_IAC_SEEN
            elif (self.suboption is not None):
                self.suboption += byte
            else:
                (yield byte)
        elif (self.mode == M_IAC_SEEN):
            if (byte == IAC):
                if (self.suboption is not None):
                    self.suboption += byte
                else:
                    (yield byte)
                self.mode = M_NORMAL
            elif (byte == SB):
                self.suboption = bytearray()
                self.mode = M_NORMAL
            elif (byte == SE):
                self._telnet_process_subnegotiation(bytes(self.suboption))
                self.suboption = None
                self.mode = M_NORMAL
            elif (byte in (DO, DONT, WILL, WONT)):
                self.telnet_command = byte
                self.mode = M_NEGOTIATE
            else:
                self._telnet_process_command(byte)
                self.mode = M_NORMAL
        elif (self.mode == M_NEGOTIATE):
            self._telnet_negotiate_option(self.telnet_command, byte)
            self.mode = M_NORMAL
