optimize flush packets

This optimizes flush_packets(). Instead of looping over all packets in the send buffer, just loop over the ones we know needs sending. This patch almost doubles the send rate on a raspberry pi (you get a lot of cache misses with the previous code).

This particular patch hasn't been well tested, it was pulled out of a larger patch set which was though.