move flushing buffer logic into function
move lws update at the top of for
write_buf_check_n_flush moved upwards
add assert tun dev not selected