Backporting a fix from socketstream#387 into the library