title: Decode a websocket frame
body: <p>I am trying to decode a websocket frame, but I'm not successful when it comes to decoding the extended payload. Here what I did achieve so far:</p>
  
  <pre><code>char *in = data;
  char *buffer;
  unsigned int i;
  unsigned char mask[4];
  unsigned int packet_length = 0;
  int rc;
  
  /* Expect a finished text frame. */
  assert(in[0] == '\x81');
  packet_length = ((unsigned char) in[1]) &amp; 0x7f;
  
  mask[0] = in[2];
  mask[1] = in[3];
  mask[2] = in[4];
  mask[3] = in[5];
  
  if (packet_length &lt;= 125) {           **// This decoding works**
       /* Unmask the payload. */
      for (i = 0; i &lt; packet_length; i++)
          in[6 + i] ^= mask[i % 4];
      rc = asprintf(&amp;buffer, "%.*s", packet_length, in + 6);
  } else
      if (packet_length == 126) {       **//This decosing does NOT work**
          /* Unmask the payload. */
          for (i = 0; i &lt; packet_length; i++)
            in[8 + i] ^= mask[i % 4];
          rc = asprintf(&amp;buffer, "%.*s", packet_length, in + 8);
  }
  </code></pre>
  
  <p>What am I doing wrong? How do I encode the extended payload?</p>