Ensure connection exists before writing to it

Change-Id: I96f310b7eccbd3f868cd6a833cee0f6a42a51244