Use RFC 1459 http://www.mirc.co.uk/help/rfc1459.txt

Everything you need is in there