def _zmq_socket_context(context, socket_type, bind_endpoints):
    """A ZeroMQ socket context that both constructs a socket and closes it."""
    socket = context.socket(socket_type)
    try:
        for endpoint in bind_endpoints:
            try:
                socket.bind(endpoint)
            except Exception:
                _logger.fatal("Could not bind to '%s'.", endpoint)
                raise
        yield socket
    finally:
        socket.close()
A ZeroMQ socket context that both constructs a socket and closes it.