Reworked connect/disconnect code.
Fixed buf_free() functions.