I surely hope nothing like that exists. For some ports it may not be that drastic, but what if I write an application that tries to bind on port 80? Boom! No more webserver. Port 135? Boom! File sharing is gone.
Instead you should close your application nicely, so it will unbind the port itself.
And by the way, port 4000 is reserved for something called Terabase. See IANA port numbers.
It's _still_ bad. If you track down say port 80 and then destroy that, then you are destroying the default web server's ability to be connected to! What you need to do is have a snippet of code that tries to connect to a port that is free, so in other words have a loop that connects to a port, checks if it gets a Bind exception, if it does then try another port.
I understood your logic. But, the same code will be used in different nodes in cluster of servers. So, I will assign a specific port in harcoded and all the server should able to create ServerSocket in that port only, it shuold not through any BindingException.