@Nick Gammon, the compiler complains that some of the variables are undeclared (as if maybe I need to #include something).
@bperrybap, shouldn't the flush at the beginning wait till there is only one byte left to send? At that point, there should only be 1 byte, so shouldn't it work fine? If not, can you please post the changes you made to the HardwareSerial library?
@eried the code you posted is a way to wait until an entire string has been received not sent.