Big bug where I was setting the payload length do be the pre-encoded size rather than the encoded size.