how to detect port open status?

I write a script which will stop an application, then restart it.

Sometimes it is succesful, sometimes not.

The problem is, when stop the application, some ports are still listenning (or not released). When start the application, it reports that ports are used, and can't continues.

I use

"Sleep 60"

to delay the start job, but seems somtime I need wait for more than 5 minutes to release the ports.

Are there any way to detect the ports, and wait until the ports are all released?

My env is Solaris 10.

Why don't u check with netstat

netstat -an | grep $port
if [ $? -eq 0 ]
then
    echo "$port is free"
else
    echo "$port is listening"
fi