Properly cleanup memory of broken connections

Fixed ticket #148.