Fix connect, write, close flow