A lost job or an unexpected major medical bill can leave you facing a homeowner’s worst nightmare: foreclosure. State rules differ on how long you’ll have before a foreclosure sale takes place, however. If you find yourself in this situation, here’s what you can do.

Best ways to prevent foreclosure

Missing a house payment by a few days won’t put you in danger of foreclosure. If you end up making the payment shortly after the due date, let your lender or servicer know it was paid, albeit late.

If you still haven’t paid by the end of the grace period, however (usually 10 to 15 days), your mortgage lender has sent you past-due notices or you’re multiple mortgage payments behind, you need to act quickly to get your mortgage back in good standing and avoid foreclosure proceedings.

Here are some options your lender might offer to avoid foreclosure. You might want to seek legal advice before going any of these routes:

1. Mortgage repayment plan

If you suffer a short-term financial setback (such as expensive car repairs or a medical emergency), your lender might provide some breathing room by agreeing to let you pay off your missed payment in two installments over the next two months.

2. Loan modification

Mortgage servicers can permanently adjust the terms of your loan — most often by lengthening the amortization schedule, lowering the interest rate or rolling the delinquent amount into the loan and re-amortizing the new balance — to help you bring the loan current. This might not reduce your principal owed, however.

3. Deed-in-lieu of foreclosure

A deed-in-lieu of foreclosure involves turning over your home to a lender voluntarily to avoid foreclosure proceedings. In some instances, going this route could help you avoid paying the remaining loan balance on your mortgage, but that depends on your lender’s rules and the state you live in. Before you get a deed-in-lieu of foreclosure, ask your lender if it will waive any deficiency, which is the difference between your home’s value and what you still owe on the mortgage. (If there is a deficiency, the lender could seek a judgment to try to collect even after you’re out of the home.)

4. Short sale

A short sale happens when the lender allows you to sell the house for less than the outstanding loan amount, takes the proceeds and forgives any remaining debt. A short sale could help you salvage some of your equity, but the lender has to approve it first. A real estate agent with experience in short sales might be able to help you find a buyer and guide you through the lengthy process of obtaining the necessary approvals.

5. Short refinance

The lender forgives some of your debt and refinances the rest into a new loan. This type of refi was more common in the aftermath of the mortgage crisis, and might not be available for most homeowners now.

6. Refinance with a hard money loan

You won’t like the high rates and fees of a hard money loan — one from a private lender, often an individual — but it might buy you some time to sell your home and avoid foreclosure. This shouldn’t be your first option, though, if you can help it.

How to contact your lender

Lenders want their money repaid in a timely way and the interest that comes with it; they don’t want your house. If you seem to be a good risk, the lender will offer to help keep your mortgage afloat, but be forewarned: If you seem like a bad risk, the lender might cut its losses by taking steps to foreclose and evict you as quickly as possible.

The key is to communicate with the lender before your debt gets the better of you. The sooner your lender knows of your problem, the more help it can provide. As you work to find mortgage relief, keep copies of all communications with your servicer, as well as records of who you communicated with. Follow up every phone call in writing, too.

Federal law requires mortgage servicers to help delinquent borrowers and work with them to get back in good standing. Tell your bank or lender that you want to learn about options for “loss mitigation,” the technical term for avoiding foreclosure.

Also, look for a letter from your lender describing options for avoiding foreclosure, along with instructions and applications for any programs that might apply to you.

Your mortgage servicer should also provide a contact person available by phone to answer your questions and provide accurate information about your options for avoiding foreclosure. By law, this person should be assigned to you within 45 days after your loan becomes delinquent, according to the Consumer Financial Protection Bureau.

If you feel you aren’t getting proper assistance from your mortgage servicer, file a complaint online with the CFPB, or call (855) 411-CFPB.

If you aren’t getting anywhere with your mortgage company, you can obtain free advice and support from a housing counselor sponsored by the U.S. Department of Housing and Urban Development (HUD). An expert from a housing counseling agency can guide you as you try to work with your mortgage company to avoid foreclosure.

You can find a local HUD-approved expert online, or call HUD’s Office of Housing Counseling at (800) 569-4287. If you have an Apple device, you can download HUD’s free Housing Counselor Locator app.

Beware of foreclosure rescue scams

Unfortunately, scammers masquerading as legitimate housing counselors often try to take advantage of homeowners who are vulnerable. As you work through your mortgage issue, keep a skeptical eye out for fake programs and scams.

Warning signs of a scam include organizations that require advance payment or that guarantee to fix your foreclosure problems.

In particular, beware of offers to help you get a loan modification through the federal HAMP Program, a federal loan modification program that expired at the end of 2017. In the past, fraudsters have told homeowners that they were approved for the program as a way to trick them into sending money.

Also watch out for phone calls or mail solicitations that appear to be from your mortgage company, but direct you to send payments to an unfamiliar address that doesn’t match the one on your mortgage statement.

If you’re having difficulty making your house payments, your best bet is to reach out to your lender and a HUD-approved housing counselor as soon as possible. This will improve your chances for a happy resolution that helps you avoid the financial and emotional pain of foreclosure.