 {
   if(*c == 27)
   {
-    *c=stream->read();
+    *c=streamAvailRead(stream);
+    logSocketIn(*c);
     if(*c=='[')
     {
       int code1=0;
       int code2=-1;
-      *c=stream->read();
+      *c=streamAvailRead(stream);
+      logSocketIn(*c);
       while((*c>='0')&&(*c<='9'))
       {
         code1=(code1*10) + (*c-'0');
-        *c=stream->read();
+        *c=streamAvailRead(stream);
+        logSocketIn(*c);
       }
       while(*c==';')
       {
-        *c=stream->read();
+        *c=streamAvailRead(stream);
+        logSocketIn(*c);
         if((*c>='0')&&(*c<='9'))
           code2=0;
         while((*c>='0')&&(*c<='9'))
         {
           code2=(code2*10) + (*c-'0');
-          *c=stream->read();
+          *c=streamAvailRead(stream);
+          logSocketIn(*c);
         }
       }
       switch(code1)

    }
    if(*c==TELNET_WILL)
    {
      char what=stream->read();
      uint8_t iacDont[] = {TELNET_IAC, TELNET_DONT, what};
      if(what == TELNET_TERMTYPE)
        iacDont[1] = TELNET_DO;
      stream->write(iacDont,3);
      return false;
    }
    if((*c==TELNET_WONT)||(*c==TELNET_DONT))
    {
      char what=stream->read();
      return false;
    }
    if(*c==TELNET_DO)
    {
      char what=stream->read();
      uint8_t iacWont[] = {TELNET_IAC, TELNET_WONT, what};
      if(what == TELNET_TERMTYPE)
        iacWont[1] = TELNET_WILL;
      stream->write(iacWont,3);
      return false;
    }
    if(*c==TELNET_SB)
    {
      char what=stream->read();
      char lastC=*c;
      while(((lastC!=TELNET_IAC)||(*c!=TELNET_SE))&&(*c>=0))
      {
        lastC=*c;
        *c=stream->read();
      }
      if(what == TELNET_TERMTYPE)
      {
        stream->write(TELNET_IAC);
        stream->write(TELNET_SB);
        stream->write(TELNET_TERMTYPE);
        stream->write((uint8_t)0);
        stream->write(termType.c_str(),termType.length());
        stream->write(TELNET_IAC);
        stream->write(TELNET_SE);
        return false;
      }
    }

  /** TELNET CODE: IAC*/
#define TELNET_IAC 255 


uint8_t streamAvailRead(Stream *stream)
{
  int ct=0;
  while((stream->available()==0)
  &&(ct++)<1000)
    delay(1);
  return stream->read();
}

bool handleAsciiIAC(char *c, Stream *stream)
{
  if(*c == 255)
  {
    *c=streamAvailRead(stream);
    logSocketIn(*c);
    if(*c==TELNET_IAC)
    {
      *c = 255;
