You need to consider how you want your code to flow on a failed connect. Right now, all you connect statements are of the form
connect or die, so naturally when a connect fails, your die handler gets called and your script dies. So, rather than calling
die, you should use some other form of flow control. If you want exit your current subroutine, perhaps
connect or return undef; would suit your needs. If you are in a loop and wish to advance to the next cycle,
connect or next; might do what you want (see
continue or
Foreach Loops for implementation details). Once you've decided how you'd like your script to behave following a failed connect, I'd be happy to help you figure out implementation.
Update: Disregard, as the heart of your issue is addressed by almut below.