Using the "on connection" routine can be hazardous if the server you're connecting to is a "bounce" server, which will immitate your normal server, which would leave this script to think it's actually connecting to the real server, giving away your information. It's an old IRC hack & this script sorta opens that weakness.
I suggest setting a variable with the connection ID ($cid) and then checking with that, eliminating possibilites of a hack:)