Ensure that ThreadedHTTPTransport sends all pending messages at exit

A race condition in the ThreadedHTTPTransport exit hook meant that it was
possible for some queued messages to not be sent.

Rearrange the shutdown hook a bit to ensure that this cannot happen.