Fix infinite loop on connection error, closes #1