added buffer locking when closing connection. Fix #17