Handle connection shutdowns correctly.

Change-Id: I92c123eb0564847701beb45c9ee42362572768c8