Modified defer position as it could cause a panic if invoked after a connection error happened