Actually drop to C and use ntohs() to decode lengths (nothing portable is available in the base libs).