Fix some crashes in connection when something goes wrong.