Wrote a keep alive thread. Should be thread safe.
Added an atomic flush.