Updated connection closing in error cases