Fix race condition between messages.insert, resendRecent and send

Previously, a connection handshake and therefore a resend loop
could happen in parallel with sends. This is problematic, because
resend first looks up the messages from db and then sends them
one bye one. If a message is added to db, it will not get sent
in the resend function, and if the actual send fails, it will
be sent neither over the old or the new connecion, resulting in
a deadlock until the next resend.