Be more careful with locks

It was possible for the _lostConnection method to be called from
within the _onConnect method after a particularly unfortunate
series of events.  In that case, the connection would not have
moved from the inactive to active list, and _lostConnection would
throw an Exception from within a critical section holding a lock.

Explicitly handle that case with some conditional statements, but
also add try/finally clauses around any critical sections which
do any non-trivial work.

Change-Id: Id33c2fad6fd11a6013689525e7dbdf9624e45360
Merge "Be more careful with locks"