Untrack the connection before closing to avoid possible double-free

From Gangadharan S.A. Fixes issue #166.