The housing market is cooling off as high mortgage rates and inflation threaten homebuyer demand. That could spell trouble for real estate stocks, especially those with direct ties to the housing market.

Nonetheless, a downturn can be an excellent opportunity for long-term investors to acquire shares of proven winners. Here are two shining examples of real estate excellence that will likely continue thriving over the years to come.

1. The king of home improvement

The Home Depot (HD 0.02%) is one of America's largest retailers and a place where you can buy hardware, tools, materials, and appliances to maintain and upgrade your home to your wildest dreams. Home Depot isn't just for the DIY adult; it also sells to professional contractors and home builders, giving it exposure to a broad swath of spending in the housing sector.

Home Depot enjoys built-in growth from long-term trends in population growth and housing. For example, the total supply of homes in the U.S. has grown from roughly 120 million to 140 million over the past 20 years. Add in the tendency for owners to remodel an existing home over and over, and you have recurring revenue for Home Depot. The company's only meaningful decline in business came during the financial crisis in 2008-2009, and the company's a strong cash generator, converting almost 10% of its sales into free cash flow.

HD Revenue (TTM) Chart.

HD Revenue (TTM) data by YCharts.

Strong and profitable growth drives shareholder returns over the long term, and Home Depot is no slouch here. A $10,000 investment made at the company's IPO would be worth an eye-popping $248 million today. The company's market cap is now a hefty $330 billion, so investors should temper expectations moving forward.

However, Home Depot can still be a winner in a long-term portfolio. The company's recent momentum is strong, growing sales by an average of 10% annually over the past five years. Meanwhile, the home improvement market is vast and continues growing; Statista estimates that the home improvement market in the U.S. could touch $620 billion by 2025, up from $538 billion in 2021. 

2. America's top homebuilder

The number of new houses built in the U.S. plummeted in July, which could be the sign of a cooling market due to a combination of higher mortgage rates and soaring home prices dumping water on buyer demand. That could impact D.R. Horton (DHI 0.10%), the largest homebuilder in America by volume. The company builds in 105 markets across 33 states.

The company has grown steadily over the years, but you can see the acceleration in growth over the past two, a direct benefit of a red-hot housing market. But the stock's always been a winner, turning a $10K investment at IPO into $617K today.

DHI Revenue (TTM) Chart.

DHI Revenue (TTM) data by YCharts.

The short term could get a little rocky if the housing market continues deteriorating over the coming months. However, D.R. Horton is poised to endure any pain that might come its way. The business is sitting on cash and equivalents of $1.65 billion, and there is just $1.05 billion in debt coming due between now and the end of 2024. The business could hit a real rough patch, and the balance sheet should still hold up.

Meanwhile, it's not likely that the company will see a doomsday type of scenario. Management acknowledged the potential uncertainty moving forward but still expects $33.8 billion to $34.6 billion in revenue for the entire 2022 year, a 24% increase over 2021. There is a consensus that there's not enough housing in the U.S, a potential driver for healthy long-term demand. If the housing market does hit the skids and investors sell out of fear, D.R. Horton could take a short-term hit. But with rock-solid financials and a leading market position in the housing market, it's still poised to produce solid investment returns over the long term.