Imagine that the XYX (not a real name) stock has a bid of 50.05 and an ask of 50.12. A discount broker currently has two orders. One is a limit buy at 50.11 and the other is a limit sell at 50.06. Can the broker buy the stock from the first client at 50.06 (using the broker's money ) and then immediately resell it to the other client for 50.11?
This would be frontrunning. Stock brokers generally have a fiduciary duty to their clients, and front running someone you have a fiduciary duty towards is illegal.
Front running, also known as tailgating, is the prohibited practice of entering into an equity (stock) trade, option, futures contract, derivative, or security-based swap to capitalize on advance, nonpublic knowledge of a large ("block") pending transaction that will influence the price of the underlying security. In essence, it means the practice of engaging in a Personal Securities Transaction in advance of a transaction in the same security for a client's account. Front running is considered a form of market manipulation in many markets.
No. A Stock Broker is not trading anything - all he is is forwarding orders to the exchange. If he does not do that he is actually committing fraud - at least because he invoices you the fee the exchange calls for it (unless you are having one of those "free" trading accounts).
Trading stocks is regulated in pretty much every jurisdiction and unless you talk of some really low end OTC stocks these days where the broker may work together with a dealer / market maker, no - he simply is not trading to start with. Brokers are basically (especially these days) glorified call center agents or (as organizations) order forwarding systems. At least unless you are really rich.
The SEC defines Internalization as:
When you place an order to buy or sell a stock, your broker has choices on where to execute your order. Instead of routing your order to a market or market-makers for execution, your broker may fill the order from the firm's own inventory. This is called "internalization." In this way, your broker's firm may make money on the "spread" – which is the difference between the purchase price and the sale price.
Linked to the above page is another page which explains Your Broker Has Options for Executing Your Trade:
Just as you have a choice of brokers, your broker generally has a choice of markets to execute your trade:
For a stock that is listed on an exchange, such as the New York Stock Exchange (NYSE), your broker may direct the order to that exchange, to another exchange (such as a regional exchange), or to a firm called a "third market maker." A "third market maker" is a firm that stands ready to buy or sell a stock listed on an exchange at publicly quoted prices. As a way to attract orders from brokers, some regional exchanges or third market makers will pay your broker for routing your order to that exchange or market maker—perhaps a penny or more per share for your order. This is called "payment for order flow."
For a stock that trades in an over-the-counter (OTC) market, such as the Nasdaq, your broker may send the order to a "Nasdaq market maker" in the stock. Many Nasdaq market makers also pay brokers for order flow.
Your broker may route your order – especially a "limit order" – to an electronic communications network (ECN) that automatically matches buy and sell orders at specified prices. A "limit order" is an order to buy or sell a stock at a specific price.
Your broker may decide to send your order to another division of your broker's firm to be filled out of the firm's own inventory. This is called "internalization." In this way, your broker's firm may make money on the "spread" – which is the difference between the purchase price and the sale price.
Brokers must adhere to best execution practices that are regulated by the SEC and individual exchanges. What I do not know is what limitations are placed on brokers when it comes to Internalization.