Windows will sometimes configure multiple MIDI ports if the same interface is attached to differing USB sockets. When the device is disconnected or turned off Windows might that port as a "hardware not currently attached" ghost. This is a problem particularly if you have a version of Windows that only allows 10 or so MIDI ports to be present on the system. I think (but I'm not certain) that Windows 8 onwards don't have that problem. I have more than ten ports running on Windows 8.1.
Windows will sometimes do the same thing for all kinds of USB hardware, which is why it sometimes runs its "device found, installing driver" routine when you plug in hardware that's already been used on that system.
If you go to Control panel/device manager and select "show hidden devices" in the view menu it will show you the devices Windows has kept as ghosts.
As for Sonar dropping the MIDI hardware when it's switched off or disconnected, that makes perfect sense. Sonar has noticed that a device it was in contact with can no longer be reached so warns you about it. When it sees the device again it asks whether you want Sonar to connect to it or not. Bear in mind that, for example, Sonar doesn't know the difference between MIDI hardware and MIDI software such as MIDIoX that also open MIDI ports in Windows. Sonar just sees MIDI ports presented to it by Windows. There may be perfectly good reasons why you need to run MIDIoX (or other MIDI devices) but not want Sonar to grab those MIDI ports.