If your script gets run from scratch on every call, and you can't change that, it looks like you're going to have to split it into a client-server architechture. That means, have one piece (the server) that runs all the time, holding the AIM connection open, and listening for things to do from the other piece (the client), which gets rerun every time, and contacts the server script with whatever it has to send.
There are several ways to communicate between client and server, try reading up on sockets and IPC in the perlipc manpage.
C.