How does Visual Studio decide which port to run applications on?

asp.net, visual-studio, visual-studio-2005

Solution

By default, ASP.NET Web Application Projects are configured to launch and run using the built-in VS Web Server (aka Cassini) on a random HTTP port on the machine.

This port number can be changed if this port is already in use, or if you want to specifically test and run using a different number:

(source: scottgu.com)

Problem

When you run an application from within visual studio, it seems to pick a random port on localhost. For example, running an application right now, I get http://localhost:3240/ApplicationStuffHere I realize this port gets loaded into the solution file, via VWDPort = "3240" How does this get decided on? it seems fairly random.

Original source