Backport of test and fix for zeromq/libzmq#1015

Test case by: Joe Eli McIlvain <joe.eli.mac@gmail.com>
Bug fix by: Martin Hurton <hurtonm@gmail.com>