network changed event
rewrite mqtt loop

Replace the builtin background loop with our own, which can detect
network changes. We could've kept using Mosquitto's loop, but it has a
couple of problems:
- It throws some unlovely exceptions when ssl is enabled and the network
  is down.
- It doesn't let us do an exponential backoff on reconnect attempts.