[core] Fix error with ping timeout handling

This error was caused by terrible line-breaking that didn't only
make the code unreadable, but also broke it.

Whoever merged the patch to do that didn't test their code, and the
people who wrote it clearly didn't either.

I also took the time to simplify handle_close, and make sure the
ping thread doesn't die on socket errors, and doesn't send pings
if the bot is not connected yet.

Fixes #539