The easiest way to detect when you are connected is to use RAW 001 (to 004).
RAW 001:*: echo -s Now connected
Raw 001
-------
Format : 001 :Welcome to the Internet Relay Network <nickname>
Example: 001 :Welcome to the Internet Relay Network Gemini
Network: All
Parameters
----------
<nickname> Your nickname.
Info
----
Sent to all clients once a connection has been established and the user has successfully registered.
Notes
-----
This is the first point where a script or client can be sure that the user's connection has been accepted without errors or problems.
This reply is sent once upon connection automatically, and never again during an IRC session.
Variations
----------
On many networks (such as DALnet) the user's full address will be given instead of just a nickname, for example-
001 :Welcome to the Internet Relay Network Gemini!zodiac@port123.stupid.com
Also, many networks change the message itself to include the network's name in the message, for example-
001 :Welcome to the DALnet IRC Network pai!goddess@21.9.234.32
You cannot be guaranteed that the network name or user's nickname/address will always be in the same position.
See Also
--------
The following are sent in sequence on connection-
001, 002, 003, 004
-genius_at_work