Handle disconnections and other error cases properly.