Yes, you can apply, but it's not really a program that has a high success rate. The foreclosure can be stayed if you are in the process. Whether it will actually work at the end of the day is something that's hard to say.
Go for it. Some people have a better chance (luck) of getting a loan modification after filing bankruptcy because the bank knows that they are protected from a deficiency. It almost puts you in a better bargaining position. Apply through HAMP, and even the bank's in-house program if HAMP doesn’t work. I could give you a complicated answer regarding your second mortgage, but I'll just say don't worry about them right now. They continue to have a lien on the property, but it shouldn't stop you from applying for a loan modification with your first lender. The first lender can continue the foreclosure. They are only required to stay foreclosure if you enter into a trial modification and are making payments, or stay the sale until you are properly considered for a HAMP (and only applies to HAMP) modification. See section 1508(d-5) of the IMFL.
The information in this answer is not intended as legal advice nor do I intend to create an attorney-client relationship with any reader simply by answering this question or contributing as a member of AVVO.