added missing connection.close() in some corner cases.