Troy, I am a police officer since 1998 and during my career I have become a firearms instructor and have been trained in death investigations including homicide. My point of view is different than others mentioned and based on my training and experiences. When I started out in my career I carried a Beretta 92FS in 9mm, back then I had to supply my own gun and chose this because of the reliability and magazine capacity. As the years progressed I carried a Beretta 96D and Beretta Brigadier both of which were chambered in .40 S&W and now I carry a .45 GAP. At times I carry a 10mm Colt Delta Elite off duty.
First the idea of stopping power is a lot of crap in my professional opinion. In the real world when people are shot they are not knocked off their feet, have huge gaping wounds, or die instantly. The reason I say this is back in the late 80s the FBI was involved in a shoot out in Florida with two bank robbers which several agents were hurt and killed. Back then most of the agents involved were carrying either 9mm, 38 Specials, or 357 Magnums. The shoot out was extremely violent (which is expected) and went on for over five minutes, lots of rounds were exchanged during this time. When they autoposies were done on the bad guys it was determined that the one guy took approximately a dozen (I don't remember the exact number at this time) rounds that were classified as fatal hits. This sounds unbelievable, and it did to me at the time when I was sitting in the class, until you find out the rest of the story. Both of these individuals were hopped up on drugs, some of which were Cocaine and Barbitiuates. These drugs both have a significant affect on the human body seperately and it only increases with the combination of the two along with the other drugs they had in their systems. In light of the investigation that followed the FBI recognized several areas of concern, one of which was the guns and ammunition they carried. Out of this the FBI adopted the 10mm but as time went on some agents had trouble shooting and handling this round. Then Smith and Wesson in conjunction with Winchester developed the .40 S&W, which in very basic terms is the short version of the 10mm. Once this happened there was a major wave of law enforcement agencies that went with and continue to carry the .40 S&W because it's more managable and easier to get ammuntion opposed to the 10mm. Sig decided to make the .357 Sig, which as stated earlier is a .40 S&W necked down to 9mm to increase velocity. It's a good round but really explensive to shoot and hard to find ammuniton opposed to the 9mm and .40S&W. Lately Glock came up with the .45 GAP which is intended to have the same ballistics as the .45 Auto in a gun that has the frame size of the 9mm and less recoil. Currently the Pennsylvania, New York, and several other state police agencies adopted the .45 GAP. One other advantage the .45 GAP has over the .40 S&W is there is noticibily less recoil than the .40 S&W because it generates far less chamber pressure.
All that being said, my first piece of advice is get QUALITY AMMUNITION. There is a lot of stuff out there that claims to be the super bullet and will never fail. I have to say in all the ballistic tests I've seen all bullets will fail at some point. My first choice in ammunition is the Winchester XST series and second would be the Winchester PDX series. A little secret about the XSTs is it's the politically correct version of the black tallons, pretty much the same bullet minus the black coating.
The second thing I will say is make sure what ever gun you pick if comfortable in your hand you can shoot accurately, and practice with it a lot. Three in the morning isn't the time to figure out you can't remember where the safety is nor is it the time to realize you should've been shooting more. In this situation lives are on the line, your's, your family's, the responding police officers, and even your neighbors'. Now I'm sure you're thinking what the hell does he mean my neighbors' lives are on the line. Let me explain, think of how a home is constructed, there are studs in the walls followed by gaps. Exterior walls are made the same way and there are boards, siding, bricks, or other boards on those walls. Stastically the odds of hitting a stud are relatively slim which allows the bullet, more than likely, to hit a gap and less material to penetrate to exit your home and potentially hit and even enter your neighbors' homes. Can it happen sure it can, heck people hit the multimillion dollar lotteries. No matter how remote the chances it is a factor that needs to be considered. Worse than that and much more likely is a shot misses it's target and penetrates an interior wall and exits the other side. What's on the other side of that wall, your fireplace, big screen tv, a dresser, or your wife, son, or daughter? I don't know but it's something you need to be thinking about because that bullet that enters and exits an interior wall has the ability to kill or seriously injure.
As for your question of which is better the 9mm or the .40 S&W that's a tough one. In favor of the 9mm is higher magazine capactity, cheaper practice ammunition, ammunition is easy to find, and less recoil. In favor of the .40 S&W you have more energy and heavier bullets, which penetrate deeper than lighter one. Ammuniton for the .40 S&W is easy to find also but you have more recoil which is more of a hard snap compared to the 9mm's or even the .45 GAP's push. Ballistics are neet to look at but the real world is where the metal meets the road. People wear clothes, which can be a tough barrier for a bullet to penetrate especially when it comes to heavy material and winter clothes. Also people take drugs which have a profound affect on the human body. We can debate this for the next ten years and be right were we are now, for each pro there's a con for each. In my professional opinion between the 9mm and .40 S&W I'd pick the .40 S&W. If I had my choice of any caliber in a handgun for self defense I'd probably go with either 10mm, .45 Auto, or .45 GAP. The .45 GAP ammo is tough to get at some places and expensive but when it comes to the price of a box of bullets or the price of my loved one's lives and mine price doesn't matter. As for home defense my number one choice would be a 12 gauge shotgun loaded with number 4 shot. It's very devistating at close range and if a pellet misses the intended target there is far less potential for it having leathal abilities, generally speaking, if it penetrates and exits a wall.
I know that I've been really long winded but hope this infomation helps you out. I can't get into what I've seen at work but trust me when I say you'd be amazed what bullets will and won't do. If you want to know something specific or want to know more feel free to ask.