I'm in the process of writing a socket bot, and I'd like if I could prevent excess-flood by only allowing X lines per second to be sent, and everything after should be in a buffer. I really have no idea how to accomplish this though.. Does anyone have any ideas?

Text in the buffer should be sent as soon as possible..

For example:

I want the bot to send:

PRIVMSG #channel :line 1
PRIVMSG #channel :line 2
PRIVMSG #channel :line 3

but, say I have %linespersec set to 2, it should send line 1 and line 2, and wait 1 second and send line 3 (that leaves one more message allowed to be sent within that second)